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

Shane Green shane at umbrellacode.com
Thu Jul 4 00:48:10 CEST 2013


Thank you, you are exactly right about what I meant, and yes, there is the pathological extreme–like using a semicolon to terminate every line in a  Python program.  

The approach exhibits a Pythonnistic consistency, IMHO.  The break terminator isn’t a special case, semi colons are optional just like they are for terminating statements, and the semicolons correspond functionally to where they would be had you used them to flatten the expansion onto a single line (without conversion into a comprehension).  

Lastly, while I think the terminating break will typically be one of the easier components of a comprehension to parse, I have come across some list comprehensions that were challenging to parse visually.  If my preferred approach to make them more parsable were semicolons, then applying them at several spots in the might be helpful to achieving that goal.  

My personal preference is likely to be to break apart the comprehension across lines or, if still shorter than 80 cols, use an extra space or two between sub-statements.


On Jul 2, 2013, at 8:05 PM, Joshua Landau <joshua.landau.ws at gmail.com> wrote:

> On 1 July 2013 16:21, Shane Green <shane at umbrellacode.com> wrote:
> >
> > Having a bit of a hard time following the status of this as I dropped out for a while, but I’m not really for the semi-colon separator approach.  Of all the options already available in list comprehensions, this one actually seems to be one of the most easily because it starts with a keyword and ends at the end.  Not that syntax highlighting should be taken into account in general, it’s worth noting the syntax highlighted version really makes this distinction quite clear: 
> <IMG> 
> 
> That's somewhat convincing. I'm still not convinced we need new syntax though. \
> 
> > One thing I could see doing would be to allow semi-colons anywhere in a  list comprehension that’s a boundary between statements of the expanded form. 
> >
> > Then they behave just like the optional semi-colons you could put at the end of a line.  
> >
> > Sorry if that’s precisely what’s been promoted. . 
> 
> So (I'm just adding examples to your idea):
> 
>     [item for sublist in data for item in sublist; if item]
> 
> can become
> 
>     [item; for sublist in data; for item in sublist; if item]
> 
> 
> In a pathological case this is a transform from:
> 
>     [item for sublist in data if sublist for item in sublist if item%3 if item-1]
> 
> to
> 
>     [item for sublist in data; if sublist; for item in sublist; if item%3; if item-1]
> 
> But you can always do:
> 
>     [item
>      for sublist in data if sublist
>      for item in sublist if item%3
>                          if item-1]
> 
> 
> That said, it's not like people *do* those sorts of pathological expressions, is it?

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20130703/c2cd9efe/attachment-0001.html>


More information about the Python-ideas mailing list