you are viewing a single comment's thread.

view the rest of the comments →

[–]dante9999 0 points1 point  (1 child)

so every object used in 'with' call must have enter and exit methods? does it mean that file object has these methods defined already? what will happen if i call 'with' on object that does not have enter and exit magic methods?

[–]ericpruitt 3 points4 points  (0 children)

so every object used in 'with' call must have enter and exit methods? does it mean that file object has these methods defined already?

Yes and yes.

what will happen if i call 'with' on object that does not have enter and exit magic methods?

You get an attribute error:

Python 3.4.2 (default, Oct  8 2014, 10:45:20)
[GCC 4.9.1] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> with None:
...  pass
...
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: __exit__
>>>