For review: PEP 343: Anonymous Block Redux and Generator Enhancements
Steven Bethard
steven.bethard at gmail.com
Sun Jun 5 19:30:12 EDT 2005
Nicolas Fleury wrote:
> Since the current syntax would be there, the no-indentation syntax can
> be explained in terms of the indentation syntax:
>
> """
> To avoid over-indentation, a with-statement can avoid defining a new
> indentation block. In that case, the end of the with block is the end
> of the current indentation block.
>
> with EXPR as VAR
> REST OF BLOCK
>
> is equivalent to
>
> with EXPR as VAR:
> BLOCK
> """
>
> What do you think? I fail to see the complexity...
I guess my only real qualm about this is that I think it makes it harder
to see where __exit__() methods are called. When I compare:
def func(arg, baz):
foo = bar(arg)
with x as foo(baz)
x.frobble()
with y as x.bop()
return frabble(x, y)
with:
def func(arg, baz):
foo = bar(arg)
with x as foo(baz):
x.frobble()
with y as x.bop():
return frabble(x, y)
I find it much easier to identify in the second one that __exit__()
methods will be called right before the function returns (after the
return statement). YMMV.
BTW, if you really like the optional-indentation idea, you should post
it to the Wiki page (http://wiki.python.org/moin/WithStatement) --
Guido's been pretty quick to respond to any comments made there, so you
could get some feedback much more useful than I can give you. ;)
STeVe
More information about the Python-list
mailing list