At 11:48 PM 7/3/2005 -0400, Raymond Hettinger wrote:
Remember, these methods are going to show up in objects such as Context which are not primarily about 343. All of the other methods names will have nothing to do with 343, so our choice of magic names needs to be really good (as there will likely be NO contextual hints).
with context_expression as variable: # perform actions within a context
The "with" statement establishes a context in which some operations are to be performed. Often, this is a resource management context, wherein some resource is allocated when the context is entered, and when it is exited. Or it may be a locking context, where a lock is acquired and released around the statements. Or it may be a computational context, such as a Decimal context that controls the precision of decimal calculations. In fact, it may be any context that can be defined in terms of behavior to be performed when the context is entered and exited.
The object produced by 'context_expression' must have __enter_context__ and __exit_context__ methods, which will be invoked by the "with" statement when the context is entered, and when it is exited for any reason (such as by an exception, "return" statement, or other control flow statements).