0

I've just observed that the following little Python 3.8 program

import pickle

with open(False, "rb") as file:
    print("Inside context block")
    o = pickle.load(file)
print("Out of the context block")

outputs Inside context block and freezes at pickle.load.

  1. Shouldn't open fail and raise an exception in this case?

  2. Shouldn't pickle.load fail and raise an exception in this case?

user118967
  • 4,895
  • 5
  • 33
  • 54
  • `False` is equivalent to 0 in Python. – metatoaster Apr 08 '21 at 04:58
  • 1
    According to [the docs](https://docs.python.org/3/library/functions.html#open), `open`'s first positional argument *"is a path-like object giving the pathname (absolute or relative to the current working directory) of the file to be opened or **an integer file descriptor** of the file to be wrapped"* (emphasis mine). Since `False` is equal to the integer `0`, the latter seems to apply. – kaya3 Apr 08 '21 at 05:02

0 Answers0