[Python-ideas] if-syntax for regular for-loops
dbpokorny at gmail.com
dbpokorny at gmail.com
Sun Oct 12 20:38:35 CEST 2008
On Oct 10, 12:51 pm, Arnaud Delobelle <arno... at googlemail.com> wrote:
> There is also a problem with parsing as:
>
> * the following is correct:
>
> for i in L1 if cond else L2:
> do_something()
>
> * Python's grammar is LL(1)
Not really.
for_stmt: 'for' exprlist 'in' testlist ['if' or_test] ':' suite
['else' ':' suite]
FWIW, I think this would be an entirely different discussion if
someone did all of the work first (code+docs+test code), and THEN went
to python-dev along with two or three examples from the standard
library where the new syntax would *add* clarity.
Side note: not to discourage anyone, but I happened to look at Lib/
pydoc.py and found several examples where, due to line-length, forcing
the new syntax at every available opportunity would either
substantially reduce clarity or be more or less pointless. Here are a
couple cases so you can make up you own mind.
#1
original:
for ext in ('.py', '.pyc', '.pyo'):
if os.path.isfile(os.path.join(path, '__init__' + ext)):
return True
vs. new syntax:
for ext in ('.py', '.pyc', '.pyo') if
os.path.isfile(os.path.join(path, '__init__' + ext)):
return True
vs. existing alternative:
if any( os.path.isfile(os.path.join(path,'__init__' + ext))
for ext in ('.py','.pyc','.pyo')):
return True
#2
original:
for dict in dicts:
if name in dict:
return '<a href="%s">%s</a>' % (dict[name], name)
new syntax:
for dict in dicts if name in dict:
return '<a href="%s">%s</a>' % (dict[name], name)
Cheers,
David
More information about the Python-ideas
mailing list