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

Gerald Britton gerald.britton at gmail.com
Wed Jan 21 15:38:01 CET 2009


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.

On Wed, Jan 21, 2009 at 8:39 AM,  <rdmurray at bitdance.com> wrote:
> On Wed, 21 Jan 2009 at 21:46, Steven D'Aprano wrote:
>>
>> On Wed, 21 Jan 2009 03:10:24 pm Terry Reedy wrote:
>>>
>>> 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.
>
> It wasn't obvious to me until I read this thread, but now that I know
> about it I feel a huge sense of relief.  I was never comfortable with
> extending (or reading an extension of) a list comprehension beyond the
> obvious yield/for/if pattern before.  Now I have a reliable tool to
> understand any complex list comprehension.  I would not want to lose that!
>
>>> 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.
>
> Half right.  'while x' in the proposed syntax is equivalent to 'if not x:
> break',  But IMO it goes too far to say it has similar semantics to 'while
> x' in a while loop.  Neither
>
>    while x*x<4:
>        for x in range(10):
>            yield x*x
>
> nor
>
>    for x in range(10):
>        while x*x<4:
>            yield x*x
>
> are the same as
>
>    for x in range(10):
>        if not x*x<4: break
>        yield x*x
>
> I understand that you are saying that 'while x' is used in the same
> logical sense ("take a different action when x is no longer true"),
> but that I don't feel that that is enough to say that it has similar
> semantics.  Or, perhaps more accurately, it is just similar enough to be
> very confusing because it is also different enough to be very surprising.
> The semantics of 'while' in python includes the bit about creating a
> loop, and does _not_ include executing a 'break' in the surrounding loop.
> To give 'while' this new meaning would be, IMO, un-pythonic.  (If python
> had a 'for/while' construct, it would be a different story...and then
> it would probably already be part of the list comprehension syntax.)
>
>>> So I detest the proposed change.  I find it ugly and anti-Pythonic.
>
> I'd say +1 except that I don't find it ugly, just un-Pythonic :)
>
> --RDM
> _______________________________________________
> Python-Dev mailing list
> Python-Dev at python.org
> http://mail.python.org/mailman/listinfo/python-dev
> Unsubscribe:
> http://mail.python.org/mailman/options/python-dev/gerald.britton%40gmail.com
>


More information about the Python-Dev mailing list