tags:

views:

109

answers:

2

this is i saw in someone's code:

    def __enter__(self):
        return self

    def __exit__(self, type, value, tb):
        self.stream.close()

thanks


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
+10  A: 

Using these magic methods (__enter__, __exit__) allows you implement objects which can be used easily with the with statement.

The general idea is that it makes it easy to build code which needs some 'cleandown' code executed (think of it as a try-finally block). 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:

class DatabaseConnection(object):

    def __enter__(self):
        # make a database connection and return it
        ...
        return self.dbconn

    def __exit__(self, type, value, tb):
        # make sure the dbconnection gets closed
        self.dbconn.close()
        ...

As explained above, use this object with the with statement (you may need to do from __future__ import with_statement at the top of the file if you're on Python 2.5).

with DatabaseConnection() as mydbconn:
    # do stuff
ChristopheD
@ChristopheD: I think its: with DatabaseConnection() as mydbconn:
quamrana
True, edited to correct this error.
ChristopheD
+1  A: 

I've never used it myself so I don't have any firsthand experience with the __enter__ and __exit__ methods, but this appears to be a pretty good description of what they do.

MatrixFrog