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

Gerald Britton gerald.britton at gmail.com
Wed Jan 21 16:51:39 CET 2009


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.

Note 2: this would cover itertools.takewhile but not itertools.dropwhile, AFAICS

On Wed, Jan 21, 2009 at 10:27 AM, Vitor Bosshard <algorias at yahoo.com> wrote:
> ----- Mensaje original ----
>> De: Gerald Britton <gerald.britton at gmail.com>
>> Para: rdmurray at bitdance.com
>> CC: python-dev at python.org
>> Enviado: miércoles, 21 de enero, 2009 11:38:01
>> Asunto: Re: [Python-Dev] PEP 3142: Add a "while" clause to generator expressions
>>
>> FWIW, there are a few historic languages that implement a compound
>> for-loop:  Algol 68, PL/I, SAS et al allow constructs that, if
>> translated to an equivalent (currently invalid) Python-style syntax
>> would look like this"
>>
>> for <item> in <iterable> while <predicate>:
>>   <suite>
>>
>>
>> Some also allow for an "until" keyword.  I'm not suggesting that we
>> need to do this in Python; it's just interesting to note that there is
>> some precedent for this approach.
>>
>
> Well, you could propose changing the for loop syntax (and by extension comprehensions and generators). It's a much more radical proposal, but it does keep consistency across the board, which is one of the major flaws of the PEP in its current form.
>
> BTW, there is already an "until" keyword in python, it's called "while not" ;)
>
>
> Vitor
>
>
>      ¡Todo sobre la Liga Mexicana de fútbol! Estadisticas, resultados, calendario, fotos y más:<
> http://espanol.sports.yahoo.com/
>


More information about the Python-Dev mailing list