[Python-ideas] "Iteration stopping" syntax [Was: Is this PEP-able? for X in ListY while conditionZ:]

Andrew Barnert abarnert at yahoo.com
Sat Jun 29 04:59:35 CEST 2013


On Jun 28, 2013, at 16:50, Alexander Belopolsky <alexander.belopolsky at gmail.com> wrote:

> 
> 
> 
> On Fri, Jun 28, 2013 at 7:38 PM, Shane Green <shane at umbrellacode.com> wrote:
>> ..
>> [x until condition for x in l ...] or 
>> [x for x in l until condition]
> 
> Just to throw in one more variation:
> 
> [expr for item in iterable break if condition]
> 
> (inversion of "if" and "break"reinforces the idea that we are dealing with an expression rather than a statement - compare with "a if cond else b")  

This is pretty much the same as the single-word breakif or ifbreak ideas, but has the advantage of not adding a new keyword. 

I'm not sure, without looking more carefully, whether the grammar could be ambiguous to the parser. But to a person I think it could be. We've already got two ways an "if" can appear in a comp--an if clause, or a ternary expression. Adding a third seems like it might make it harder to get the meaning just by scanning.

As a side note, I think it would really help if we came up with a couple of paradigm examples instead of using content free toy examples. Maybe:

    [line for line in f break if not line.strip() if not line.startswith("#")]

In other words "all comment characters up to the first blank line".
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20130628/7479e11c/attachment.html>


More information about the Python-ideas mailing list