3.4.9 With Statement Context Managers

New in version 2.5.

A context manager is an object that defines the runtime context to be established when executing a with statement. The context manager handles the entry into, and the exit from, the desired runtime context for the execution of the block of code. Context managers are normally invoked using the with statement (described in section 7.5), but can also be used by directly invoking their methods.


For more information on context managers, see ``Context Types'' in the Python Library Reference.

__enter__( self)
Enter the runtime context related to this object. The with statement will bind this method's return value to the target(s) specified in the as clause of the statement, if any.

__exit__( self, exc_type, exc_value, traceback)


Note that __exit__ methods should not reraise the passed-in exception; this is the caller's responsibility.

See Also:

PEP 0343, The "with" statement