This statement is designed to work with context manager objects, which support a new method-based protocol, similar in spirit to the way that iteration tools work with methods of the iteration protocol.
Basic Usage
The basic format of the with statement looks like this, with an optional part in square brackets here:
with express [as varial]: with-block
The expression here is assumed to return an object that supports the context management porotocol. This object may also return value that will be assigned to the name variable if the optional as clause is present.
Note that the variable is not necessarily assigned the result of the expression. the result of the expression is the object that supports the context protocol, and the variable may be assigned something else intended to be used inside the statement. The object returned by the expression may then run startup code before the with-block is started, as well as termination code after the block is done, regardless of whether the block raise an exception or not.
A built-in Python object has been augmented to support the context management protocol, and so can be used with the with statement.
>>> with open(r"G: est.txt") as file: for line in file: print(line)
The context Management Protocol
To implement context management, classes use special methods that fall into the operator overloading category to tap into the with statement. This interface expected of objects used in with statement is somewhat complex, and most programmers only need to know how to use existing context manager.
Here’s how the with statement works:
- The expression is valuated, resulting in an object known as a context manager that must have __enter__ and __exit__ method.
- The context manager’s __enter__ method is called. The value it return is assigned to the variable in the as clause if present, or simply discarded otherwise.
- The code in the nested with block is executed.
- If the block raises an exception, the __exit__(type, value, traceback) method is called with the exception in the Python manuals and later in this part of the book. If the method returns a false value, the exception is raised. Otherwise, the exception is terminated. The exception should normally be reraised so that it is propagated outside the with statement.
- If the with block does not raise an exception, the __exit__ method is still called, but its type, value and traceback arguments are passed in as None.
An example of context management in action:
>>> class TraceBlock(object): def __enter__(self): print("call __enter__ function") return self def __exit__(self, exception_type, exception_value, exception_trace): print("call __exit__ function") print("exception info: type:%s value:%s" % (exception_type, exception_value)) def action(self, value): print("call action method") print(value) >>> with TraceBlock() as I: print("start") I.action("exit normally") print("end") call __enter__ function start call action method exit normally end call __exit__ function exception info: type:None value:None >>> >>> with TraceBlock() as I: print("start") I.action("raise exception") pritn("end") #not call the block call __enter__ function start call action method raise exception call __exit__ function exception info: type:<type 'exceptions.NameError'> value:name 'pritn' is not defined Traceback (most recent call last): File "<pyshell#684>", line 4, in <module> pritn("end") #not call the block NameError: name 'pritn' is not defined >>>