For review: PEP 343: Anonymous Block Redux and Generator Enhancements
Steven Bethard
steven.bethard at gmail.com
Sun Jun 5 01:40:45 EDT 2005
Nicolas Fleury wrote:
> I prefer the optional-indentation syntax. The reason is simple (see my
> discussion with Andrew), most of the time the indentation is useless,
> even if you don't have multiple with-statements. So in my day-to-day
> work, I would prefer to write:
>
> def getFirstLine(filename):
> with opening(filename) as file
> return file.readline()
>
> than:
>
> def getFirstLine(filename):
> with opening(filename) as file:
> return file.readline()
One of the beauties of PEP 343 is that it can be explained simply in
terms of current Python syntax. The expression:
with EXPR as VAR:
BLOCK
is equivalent to:
abc = EXPR
exc = (None, None, None)
VAR = abc.__enter__()
try:
try:
BLOCK
except:
exc = sys.exc_info()
raise
finally:
abc.__exit__(*exc)
Can you do the same thing for your proposal? As I understand it you
want some sort of implicitly-defined BLOCK that starts the line after
the with statement and runs to the end of the current block...
Do you think the benefit of less indentation outweighs the added
complexity of explaining the construct? I still can't think of a good
way of explaining the semantics you want. If you could provide an
explanation that's simple and as explicit as Guido's explanation in PEP
343, I think that would help your case a lot.
STeVe
P.S. I think it's a great sign that people are mainly squabbling about
syntax here. More likely than not, Guido's already made his mind up
about the syntax. So, since no one seems to have any real problems with
the semantics, I'm hopeful that we'll get a direct implementation of PEP
343 in the next Python release. =)
More information about the Python-list
mailing list