PEP-xxx: Unification of for statement and list-comp syntax
Carl Banks
invalidemail at aerojockey.com
Sun May 21 19:13:01 EDT 2006
Edward Elliott wrote:
> Special cases aren't special enough to break the rules. (proposal eliminates
> the current special case for comprehensions/generators)
It really isn't a special case, though. It might seem like it is, but
it's not at all when you remember the rules of equivalence between
listcomps and regular statements.
This listcomp:
[ x for x in y ]
is, by definition, semantically equivalent to this (except it's an
expression instead of a statement):
for x in y:
_.append(x)
If you have two fors in the listcomp, the second for is equivalent to a
nested for. This:
[ x for y in z for x in y ]
is, by defintion, equivalent to this:
for y in z:
for x in y:
_.append(x)
Likewise, an if gets its own nested block in the equivalent statements.
This:
[ x for x in y if x is not None ]
is, by definition, equivalent to this:
for x in y:
if x is not None:
_.append(x)
In other words, each for and if in a listcomp is equivalent to a nested
block in a regular statement. There's a regular way to nest, and
listcomp way, and they're separate. This PEP suggests we should mix
them up--now *that's* a special case.
Carl Banks
More information about the Python-list
mailing list