[Python-ideas] "while:" for the loop

Brice Parent contact at brice.xyz
Wed Sep 26 03:43:46 EDT 2018



Le 26/09/2018 à 05:36, Chris Angelico a écrit :
> On Wed, Sep 26, 2018 at 1:29 PM Mikhail V <mikhailwas at gmail.com> wrote:
>> On Wed, Sep 26, 2018 at 5:38 AM Chris Angelico <rosuav at gmail.com> wrote:
>>>
>>> I like saying while "something": where the string describes the loop's
>>> real condition. For instance, while "moar data": if reading from a
>>> socket, or while "not KeyboardInterrupt": if the loop is meant to be
>>> halted by SIGINT.
>>>
>>> ChrisA
>> if doing so, would not it be more practical
>> to write is as an in-line comment then?
>> with new syntax it could be like this:
>> """
>> while:  # not KeyboardInterrupt
>>      asd asd asd
>>      asd asd asd
>>      asd asd asd
>> """
>> Similar effect, but I would find it better at least because it would
>> be highlighted as a comment and not as a string, + no quotes noise.
> A comment is not better than an inline condition, no. I *want* it to
> be highlighted as part of the code, not as a comment. Because it isn't
> a comment - it's a loop condition.
For what it's worth, I'm not a fan of either solutions. In both cases 
(string or comment), KeyboardInterrupt seems to be the only way to get 
out of the loop, which which even if it were the case, it would breaks 
the DRY idea, because if you add in the future a reason to break out of 
the loop, you'd have to write the condition+break and update the 
comment/string to still be consistent.

I don't think `while True:` is not explicit. If you think about it, True 
will always evaluate positively (right??), so it can't be the hardest 
part of learning Python.
But in some cases, mostly when I work on tiny microptyhon projects that 
I share with non-python experts, where I avoid complicated code 
fragments like comprehensions, I usually use `while "forever":` or 
`while FOREVER:` (where forever was set to True before). In these case, 
I don't need them to understand exactly why the loop is indeed infinite, 
I just want them to know it is. But all these solutions are available 
right now, without any syntax change.

About the original proposal, even though I'm not a native English 
speaker, writing `while:` seems like an wobbling sentence, we are 
waiting for it to be completed. My mind says "while what?" and tries to 
find out if it's infinite or if it has to find the condition elsewhere 
in the code (like some kind of do...until or do...while loop).

-Brice


More information about the Python-ideas mailing list