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

Shane Green shane at umbrellacode.com
Sat Jun 29 19:32:16 CEST 2013


Reminds me of a cartoon in the new yorker: a guy is looking over the shoulder of another who is dressed futuristically, incredulously asking the futuristic guy, “you came back in time just so you could hit ‘reply’ instead of ‘reply all’?”

Made me realize my habit of accidentally doing the opposite is far better than being the habit of hitting reply-all :-)


On Jun 29, 2013, at 8:56 AM, Steven D'Aprano <steve at pearwood.info> wrote:

> It doesn't really *simplify* the standard idiom though. It just saves a line and, trivially, one indent:
> 
> while True:
>    if condition:
>        break
>    ...
> 
> And it doesn't even save that if you write it like this:
> 
> while True:
>    if condition: break
>    ...

This argument has come up before and I don’t really understand or agree with it.  It seems to: 
    - gloss over the fact this allows you to do these things in list comprehensions; 
    - argue against the existence of list comprehensions at all, rather than this extension; 
    - and leaves out everything else a list comprehension does.

Without this feature list compreshensions aren’t used to describe while loops, period; so you shouldn’t subtract the standard list comprehension contributions from the benefits realized by this change.  

[line.strip() for line in lines break if not line] 

compared to: 
messages = []
while True: 
    line = lines.pop(0)
    if not line: 
        break
messages.push(lines[0].strip())

Of course it would be easier using a for loop, which you’d then be tempted to replace with a compression...
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20130629/ab8de788/attachment-0001.html>


More information about the Python-ideas mailing list