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

Terry Reedy tjreedy at udel.edu
Wed Jan 21 05:10:24 CET 2009

Steven D'Aprano wrote:

> Another argument against the PEP was that it breaks the correspondence 
> between the generator expression and the equivalent for-loop. I had 
> never even noticed such correspondence before, because to my eyes the 
> most important term is the yielded expression,  not the scaffolding
> around it.

This was a major reason to add comprehensions.

Your not noticing a primary design principle is hardly a reason to 
abandon it.  Indeed, I claim that your ignorance shows its validity ;-).

 > In a generator expression, we have:
> yielded-expr for-clause if-clause
> while the corresponding nested statements are:
> for-clause if-clause yielded-expr
> The three clauses are neither in the same order, nor are they in reverse 
> order.

They are in the same order but rotated, with the last brought around to 
the front to emphasize it.  Did you really not notice that either?

 >I don't know how important that correspondence is to language
> implementers, but as a Python programmer, I'd gladly give up that 
> correspondence (which I don't find that great) in order to simplify 
> exiting a generator expression early.
> So I like the proposed change. I find it elegant and very Pythonic. +1 
> for me.

Ironically, in a thread cross-posted on c.l.p and elsewhere, someone 
just labeled Python's comprehension syntax as "ad hoc syntax soup". 
That currently is completely wrong.  It is a carefully designed 1 to 1 
transformation between multiple nested statements and a single 
expression.  But this proposal ignores and breaks that.  Using 'while x' 
to mean 'if x: break' *is*, to me, 'ad hoc'.

So I detest the proposed change.  I find it ugly and anti-Pythonic.

Terry Jan Reedy

More information about the Python-Dev mailing list