In a Python script, I encountered a variable that was defined inside a with statement, but that was used outside the statement, like file in the following example:
with open(fname, 'r') as file:
    pass
print(file.mode)
Intuitively I would say that file should not exist outside the with statement and that this only works by accident. I could not find a conclusive statement in the Python documentation on whether this should work or not though. Is this type of statement safe for use (also for future python versions), or should it be avoided? A pointer to this information in the Python docs would be very helpful as well.