[Python-Dev] PEP 377 - allow __enter__() methods to skip the statement body

Carl Johnson cmjohnson.mailinglist at gmail.com
Mon Mar 16 00:38:34 CET 2009

P.J. Eby wrote:

> Of course, at that point, what's the difference between:
>    with foo() as bar:
>        baz
> and...
>    @foo
>    def bar():
>       baz
> except for being slightly less verbose? (due to missing nonlocal statements,
> etc.)

That's not quite direct translation. Closer would be:

    def _(bar):
    del _

since the "bar" of the with statement is injected into the namespace
of the block. (Obviously, the foo would have be implemented
differently in the two cases in order to have it operate the same

I have thought about suggesting adding control flow to the with
statement before, since I think if done properly, it might be able to
negate the desire people have for multi-line lambdas/Ruby-style
blocks, but I do wonder if it would be too confusing to have "with"
sometimes mean "abstracting error handling code" and other times mean
"abstracted control flow." But now it looks like there's no way around
using the with-statement for control flow, since sometimes the block
needs to be skipped anyway. So, I'm +.5 on the idea.

-- Carl

More information about the Python-Dev mailing list