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

Nick Coghlan ncoghlan at gmail.com
Thu Jan 22 07:33:56 CET 2009

Gerald Britton wrote:
> OK then, what is the feeling out there about extending the "for"
> syntax in general (and by extension list comprehensions and generator
> expressions) by adding an optional while clause like this:
> for <item> in <iterable> [while [<predicate> | not <predicate>]:
>     <suite>
> The predicate would be tested after an <item> is taken from <iterable>
> and before execution of the <suite>.  If the predicate evaluates to
> false, StopIteration would be raised.  This construct would be
> equivalent to:
> for <item> in <iterable>:
>     if [not <predicate> | <predicate>]: break
>     <suite>
> Note: this is beyond what I was thinking in the first place, but has
> arisen from the ensuing discussion.

As Aahz said, this needs to go back to python-ideas or c.l.p to see if
it goes anywhere.

However, be aware that you're going to need examples from *real code*
that show improvements in correctness, readability or speed in order to
convince a sufficiently large number of the core devs and/or Guido that
such an additional wrinkle to the looping syntax is worth it.

A change being clever or cute isn't enough to justify its inclusion - it
needs to provide sufficient real world benefit to counter the cost of
the feature's development and maintenance, as well as the additional
overhead for all users of the language in learning about it.

An approach that has been used effectively in the past to argue for new
syntax or builtins is to trawl through the standard library and its test
suite looking for things that could be simplified by the proposed
addition to the language, but appropriate examples could also be drawn
from the code bases of other large Python projects (Twisted, Zope,
Django, Bazaar, Mercurial... etc).


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

More information about the Python-Dev mailing list