I saw this in someone’s code. What does it mean?
def __enter__(self):
return self
def __exit__(self, type, value, tb):
self.stream.close()
Here is the complete code.
from __future__ import with_statement#for python2.5
class a(object):
def __enter__(self):
print 'sss'
return 'sss111'
def __exit__(self ,type, value, traceback):
print 'ok'
return False
with a() as s:
print s
print s
Using these magic methods (
__enter__,__exit__) allows you to implement objects which can be used easily with thewithstatement.The idea is that it makes it easy to build code which needs some ‘cleandown’ code executed (think of it as a
try-finallyblock). Some more explanation here.A useful example could be a database connection object (which then automagically closes the connection once the corresponding ‘with’-statement goes out of scope):
As explained above, use this object with the
withstatement (you may need to dofrom __future__ import with_statementat the top of the file if you’re on Python 2.5).PEP343 — The ‘with’ statement’ has a nice writeup as well.