- Mastering Objectoriented Python
- Steven F. Lott
- 251字
- 2021-11-12 16:25:19
Managing contexts and the with statement
Contexts and context managers are used in several places in Python. We'll look at a few examples to establish the basic terminology.
A context is defined by the with
statement. The following program is a small example that parses a logfile to create a useful CSV summary of that log. Since there are two open files, we expect to see nested with
contexts. The example uses a complex regular expression, format_1_pat
. We'll define this shortly.
We might see something like the following in an application program:
import gzip import csv with open("subset.csv", "w") as target: wtr= csv.writer( target ) with gzip.open(path) as source: line_iter= (b.decode() for b in source) match_iter = (format_1_pat.match( line ) for line in line_iter) wtr.writerows( (m.groups() for m in match_iter if m is not None) )
Two contexts with two context managers were emphasized in this example.
The outermost context starts with with open("subset.csv", "w") as target
. The built-in open()
function opens a file that is also a context manager and assigns it to the target
variable for further use.
The inner context starts with with gzip.open(path, "r") as source
. This gzip.open()
function behaves much like the open()
function in that it opens a file that is also a context manager.
When the with
statements end, the contexts exit and the files are properly closed. Even if there's an exception in the body of the with
context, the context manager's exit will be processed correctly and the file will be closed.