Context Managers (“with” Statement)
While Python's context managers are widely used, few understand the purpose behind their use. These statements, commonly used with reading and writing files, assist the application in conserving system memory and improve resource management by ensuring specific resources are only in use for certain processes. This topic explains and demonstrates the use of Python's context managers.
- with "context_manager"( as "alias")(, "context_manager"( as "alias")?)*:
Context managers are defined in PEP 343. They are intended to be used as more succinct mechanism for resource management than
try ... finally constructs. The formal definition is as follows.
In this PEP, context managers provide
__exit__()methods that are invoked on entry to and exit from the body of the with statement.
It then goes on to define the
with statement as follows.
The translation of the above statement is:
Introduction to context managers and the with statement
A context manager is an object that is notified when a context (a block of code) starts and ends. You commonly use one with the
with statement. It takes care of the notifying.
For example, file objects are context managers. When a context ends, the file object is closed automatically:
The above example is usually simplified by using the
Anything that ends execution of the block causes the context manager's exit method to be called. This includes exceptions, and can be useful when an error causes you to prematurely exit from an open file or connection. Exiting a script without properly closing files/connections is a bad idea, that may cause data loss or other problems. By using a context manager you can ensure that precautions are always taken to prevent damage or loss in this way. This feature was added in Python 2.5.
Assigning to a target
Many context managers return an object when entered. You can assign that object to a new name in the
For example, using a database connection in a
with statement could give you a cursor object:
File objects return themselves, this makes it possible to both open the file object and use it as a context manager in one expression:
__init__() method sets up the object, in this case setting up the file name and mode to open file.
__enter__() opens and returns the file and
__exit__() just closes it.
Using these magic methods (
__exit__) allows you to implement objects which can be used easily
with the with statement.
Use File class:
Multiple context managers
You can open several content managers at the same time:
It has the same effect as nesting context managers:
Writing your own context manager
A context manager is any object that implements two magic methods
__exit__() (although it can implement other methods as well):
If the context exits with an exception, the information about that exception will be passed as a triple
traceback (these are the same variables as returned by the
sys.exc_info() function). If the context exits normally, all three of these arguments will be
If an exception occurs and is passed to the
__exit__ method, the method can return
True in order to suppress the exception, or the exception will be re-raised at the end of the
Note that in the second example even though an exception occurs in the middle of the body of the with-statement, the
__exit__ handler still gets executed, before the exception propagates to the outer scope.
If you only need an
__exit__ method, you can return the instance of the context manager:
Writing your own contextmanager using generator syntax
It is also possible to write a context manager using generator syntax thanks to the
The decorator simplifies the task of writing a context manager by converting a generator into one. Everything before the yield expression becomes the
__enter__ method, the value yielded becomes the value returned by the generator (which can be bound to a variable in the with statement), and everything after the yield expression becomes the
If an exception needs to be handled by the context manager, a
try..except..finally-block can be written in the generator and any exception raised in the
with-block will be handled by this exception block.