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

Ron Adam ron3200 at gmail.com
Tue Jul 2 08:03:38 CEST 2013



On 07/01/2013 10:27 PM, Daniel Robinson wrote:
> In addition to defying the ordinary order of the syntax, that would make
> this statement totally ambiguous:
>
> [f(x), f(y) for x in list1 for y in list2, while x < y]
>
> And I definitely don't think commas (other than the f(x), f(y) usage shown
> above) or semicolons are good ideas.


I agree.

There is also the problem of how to spell it with both a filter test, and 
the end test at the same time.  You can write it as one or the other with a 
stop() function, but they don't work independently.

    [x for x in values if x<50 or stop()]   # will work in 3.4

And of course this works now.

    [x for x in values if x%2==0]


To do both is tricky and took me a while to come up with...

    [x for x in values if stop(x>50) or x%2==0]

Where stop(x>50) raises StopIteration if the test is True or False 
otherwise.  Which make the 'or expression' needed even if it's just a 'or 
True'.

It should work, but seems awkward to me.


Cheers,
    Ron

















More information about the Python-ideas mailing list