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

Terry Reedy tjreedy at udel.edu
Mon Jan 19 18:51:33 CET 2009


Gerald Britton wrote:
> Please find below PEP 3142: Add a "while" clause to generator
> expressions.  I'm looking for feedback and discussion.

This was already discussed on python-ideas where it got negative feedback.

One objection, mentioned by Mathias Panzerbock and Georg Brandl, is that 
it is redundant with takewhile().  You did mention that in the PEP.

The other, posted by Steven Bethard, is that it fundamentally breaks the 
current semantics of abbreviating (except for iteration variable 
scoping) an 'equivalent' for loop.  This should have been listed in the 
PEP under Objections (or whatever the section.  I did not bother to 
second his objection there but will now.

-1

Steven summary:
"I'm probably just repeating myself here, but the reason not to do it
is that the current generator expressions translate almost directly
into the corresponding generator statements. Using "while" in the way
you've suggested breaks this symmetry, and would make Python harder to
learn."

Longer presentation:
"I think this could end up being confusing. Current generator
expressions turn into an equivalent generator function by simply
indenting the clauses and adding a yield, for example:

     (i for i in range(100) if i % 2 == 0)

is equivalent to:

     def gen():
         for i in range(100):
             if i % 2 == 0:
                 yield i

Now you're proposing syntax that would no longer work like this.
Taking your example:

     (i for i in range(100) while i <= 50)

I would expect this to mean:
[meaning, one would expect this to mean, using current rules = tjr]

     def gen():
         for i in range(100):
             while i <= 50:
                 yield i

In short, -1. You're proposing to use an existing keyword in a new way
that doesn't match how generator expressions are evaluated."

Terry Jan Reedy



More information about the Python-Dev mailing list