
On 12:56 pm, ncoghlan@gmail.com wrote:
PEP 377 is a proposal to allow context manager __enter__() methods to skip the body of the with statement by raising a specific (new) flow control exception.
Since there is a working reference implementation now, I thought it was time to open it up for broader discussion.
Why not allow a context manager to implement some other method, for the sake of argument let's say "__start__", which was invoked with a callable object and could choose to evaluate or not evaluate the statement body by simply not calling that object (or perhaps iterable, in the case of a generator)? This PEP proposes that we have two ways to deal with the body of a 'with' statement: either the body is run or not. I have always wanted to have another option: run the body later. Passing around an object representing the body of the with statement would allow for this use-case, as well as removing the ugly protrusion of yet another control-flow exception (which, as has already been noted, creates difficulties for some other python implementations).