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

Steven D'Aprano steve at pearwood.info
Wed Jan 21 11:46:46 CET 2009


On Wed, 21 Jan 2009 03:10:24 pm Terry Reedy wrote:
> Steven D'Aprano wrote:

...

>  > 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?

There are only three items, of course I noticed that there is *some* 
rearrangement of the first that leads to the second. Out of the six 
possible permutations of three items, they can all be described in 
terms of some sort of reflection, rotation or swap.


> > 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".

Is that Xah Lee? It sounds like the sort of thing he'd say.

> That currently is completely wrong.

It certainly is wrong. List comps and generator expressions are very 
elegant, at least to English speakers with a maths background (I 
personally "got" list comps once I recognised the correspondence to 
mathematical set notation. I assumed that was deliberate).



> It is a carefully designed 1 to 
> 1 transformation between multiple nested statements and a single
> expression.

I'm sure that correspondence is obvious to some, but it wasn't obvious 
to me, and I don't suppose I'm the only one. That's not a criticism of 
the current syntax. Far from it -- the current syntax is excellent, 
regardless of whether or not you notice that it corresponds to a 
if-loop nested inside a for-loop with the contents rotated outside.


> But this proposal ignores and breaks that.  Using 'while 
> x' to mean 'if x: break' *is*, to me, 'ad hoc'.

But it doesn't mean that. The proposed "while x" has very similar 
semantics to the "while x" in a while-loop: break when *not* x.

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

To each their own. I find it an elegant extension to the existing 
syntax.



-- 
Steven D'Aprano


More information about the Python-Dev mailing list