[Python-Dev] PEP 3142: Add a "while" clause to generator expressions

Nick Coghlan ncoghlan at gmail.com
Tue Jan 20 11:15:49 CET 2009


Kristján Valur Jónsson wrote:
> Are you all certain that this mapping from a generator expression to
> a foor loop isn't just a happy coincidence? After all, the generator
> statement is just a generalization of the list comprehension and that
> doesn't map quite so directly.

The mapping of the for and if clauses is identical for both generator
expressions and the various flavours of comprehension. It's only the
outer wrappings (creating a generator/dict/set/list) and the innermost
loop body (yield statement/item assignment/set add/list append) that
differ between the constructs.

As Terry noted, it's even defined that way in the language reference -
the expressions are pure syntactic sugar for the corresponding statements.

While this doesn't often matter in practice (since people tend to switch
to using the statement based versions rather than writing convoluted
multiple clause comprehensions), it's still an important symmetry that
matters greatly to Python VM implementers so any proposed changes need
to take it into account.

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia
---------------------------------------------------------------


More information about the Python-Dev mailing list