with statement was introduced in Python 2.5. It's useful when you have two related operations that must be executed as a pair with a block of code in between. Also, with the
with statement, you can allocate and release some resource exactly where you need it; for this reason, the
with statement is called a context manager. In the threading module, all the objects provided by the
release() methods may be used in a
with statement block.
So the following objects can be used as context managers for a
In this example, we simply test all the objects using the
This example shows the basic use of the
with statement. We have ...