[Python-Dev] PEP 343 - multiple context managers in one statement

Paul Moore p.f.moore at gmail.com
Wed Oct 26 14:59:34 CEST 2005


On 10/25/05, Nick Coghlan <ncoghlan at gmail.com> wrote:
> Paul Moore wrote:
[...]
> > Has the option of letting the with statement admit multiple context
> > managers been considered (and presumably rejected)?
[...]
> Not rejected - deliberately left as a future option (this is the reason why
> the RHS of an as clause has to be parenthesised if you want tuple unpacking).

Thanks. I now see that note in the PEP - apologies for missing it in
the first instance.

[...]
> The issue with that implementation is that the semantics are wrong - it
> doesn't actually mirror *nested* with statements. If one of the later
> __enter__ methods, or one of the first-executed __exit__ methods throws an
> exception, there are a lot of __exit__ methods that get skipped.
>
> Getting it right is more complicated (and this probably still has mistakes):

Bah. You're right, of course (about it being more complicated - I
can't see any mistakes :-))

I'd argue that precisely because a naive approach gets it wrong,
having your version as an example in the PEP (and possibly the
documentation, much like the itertools module has a recipes section)
is that much more useful.

Anyway, thanks for the help.
Paul.


More information about the Python-Dev mailing list