With
This lesson introduces the use of with and context management with synchronization primitives.
We'll cover the following...
With
Programs often use resources other than CPU time, including access to local disks, network sockets, and databases etc. The usage pattern is usually a try-except-finally
block. Any cleanup actions are performed in the finally
block. An alternative to the usual boilterplate code is to use the with
statement. The with
statement wraps the execution of a block of statements in a context defined by a context manager object.
Context Management Protocol
A context manager object abides by the context management protocol, which states that an object defines the following two methods. Python calls these two methods at appropriate times in the resource management cycle:
__enter__()
__exit__()
The with statement is used as:
with context-expression [as target]:
statement#1
statement#2
.
.
.
statement#n