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

Mikhail V mikhailwas at gmail.com
Wed Sep 26 14:12:09 EDT 2018


On Wed, Sep 26, 2018 at 4:46 PM Mark E. Haase <mehaase at gmail.com> wrote:
>
> On Tue, Sep 25, 2018 at 8:47 PM Mikhail V <mikhailwas at gmail.com> wrote:
>>
>> As for statistics - IIRC someone gave statistics once, but the only
>> thing I can remember [...] "while 1/True" is used quite a lot in the
>
> This proposal would be a lot stronger if you included those statistics
> in this thread and showed examples of stdlib code before/after the
> proposed syntax change.

I can't find the discussion with the statistics, but here two related
discussions:
https://mail.python.org/pipermail/python-ideas/2014-June/028202.html
https://mail.python.org/pipermail/python-ideas/2017-March/045344.html

The statistics was in one of 2016 or 2017 discussions, but I have failed to
find it. By any chance- maybe someone can do it here? I could do but it
would need to exclude comments/ strings from the search - I don't have a
such tool at hand.
Numbers for "while 1" and "while True" should suffice.

> I imagine that a new loop keyword could be introduced in
> a backwards-compatible way.

Now I don't know whom to believe.
In the first linked discussion, e.g. Mr. D'Aprano and Mr. Coghlan (from my
impression both from dev team) unambiguosly claimed that adding e.g.
"loop" as a loop token lead to necessity of excluding ALL
variables and functions/methods named "loop" from all sources.
https://mail.python.org/pipermail/python-ideas/2014-June/028206.html

So what is the real situation?
What I can tell - simple syntax highlighting systems and syntax checkers
will not be able to tell the difference between the statement initial token
and identifier.


Speaking of a new keyword: I can see only one benefit of new keyword
in this case: it can be extended with additional usage for "loop N"

    loop N:
        print ("=====")               # iterates N times

And I like the word "loop" slightly more than "repeat".

But still - even if it's technically possible without breaking something,
I am a bit skeptical, as it's a step towards *bloat*.
Keeping less keywords, especially for loops is much more important
than 'natural language' semantics.

BTW: Here is statistics for the C language:
https://stackoverflow.com/questions/20186809/endless-loop-in-c-c

It turns out that "for(;;)" is more frequent than "while 1" and "while true".
Since Python has "for" than maybe it is possible to use "for"
for the infinite loop:

    for:
       ...

And maybe even  it could be made into "for N:"?
Now:

    for N:

is SyntaxError, so it might be possible to use "for" for both cases.
What do you think?



> The meaning of "while" in natural language suggests a period of time or
> condition. It does not mean "forever". Therefore, it's not a good semantic fit.

I am not much into the "what it means in natural languages" games, as long
as it's understandable and already established token.

"while True:" is the infinite loop and the "True" or "1" does not add anything
practically useful for working with sources, period. Mostly causes unwanted
attention, as I expect expression or variable there. So basically it's noise.

"while:" on the contrary is visually clean, gives 100% difference
with "while Variable:" - it just tells : here is the start of the loop.
So the semantics would be: "while:" is an infinite loop, that's it.


Mikhail


More information about the Python-ideas mailing list