[Python-ideas] Is this PEP-able? for X in ListY while conditionZ:

Daniel Robinson gottagetmac at gmail.com
Tue Jul 2 04:28:24 CEST 2013


While this looks attractive to me, and it's definitely better to change
statement and comprehension syntax at the same time, this makes the
comprehension ambiguous to human parsing.

[f(x) for x in list if x > 10] basically can be read as

for x in list:
    if x > 10:
        f(x)

This kind of interpretation becomes critical if you nest more than two
levels. But [f(x) for x in list while x < 10] could read either as

for x in list while x < 10:
    f(x)

which is how you want it to be read, or (more in line with earlier list
comp habits):

for x in list:
    while x < 10:
        f(x)

which would be totally wrong.

I don't think this is a very serious problem (certainly not for the
interpreter), but it's a stumbling block.

On Mon, Jul 1, 2013 at 10:03 PM, Jan Kaliszewski <zuo at chopin.edu.pl> wrote:

> 2013-07-02 00:44, Oscar Benjamin wrote:
> [...]
>
>  Having a while clause on for loops is not just good because it saves a
>> couple of lines but because it clearly separates the flow control from
>> the body of the loop (another reason I dislike 'break if'). In other
>> words I find the flow of the loop
>>
>>     for p in primes() while p < 100:
>>         print(p)
>>
>> easier to understand (immediately) than
>>
>>     for p in primes():
>>         if p >= 100:
>>             break
>>         print(p)
>>
>
> +1
>
> Cheers.
> *j
>
>
> ______________________________**_________________
> Python-ideas mailing list
> Python-ideas at python.org
> http://mail.python.org/**mailman/listinfo/python-ideas<http://mail.python.org/mailman/listinfo/python-ideas>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20130701/be1b462b/attachment.html>


More information about the Python-ideas mailing list