"while:" for the loop

I suggest allowing "while:" syntax for the infinite loop. I.e. instead of "while 1:" and "while True:" notations. IIRC, in the past this was mentioned in python-list discussions as alternative for the "while True:"/"while 1:" syntax. I even had impression that there was nothing rational against this (apart from the traditional "don't change anything" principle) My opinion: 1. I think it'd definitely improve clarity. Although it's not extremely frequent statement, it still appears in algorithms, where additional noise interfers the reader's concentration. 2. This should become the answer to the "how should I denote an infinte loop?" question. 3. In schools/unis they teach algorithms with Python syntax so it will be easier to remember and to write. Adoption of this spelling is natural and straightforward. 4. It does seem to be a rare case of a very easy to implement syntax change (an expert note needed) Also I have personal sympathy for this because I like to use explicit "break" in the loop body, even though I could use "while expression:" syntax, but I prefer this: while 1: ... if i == N : break instead of this: while i < N: ... It helps me to concentrate by reading, especially in nested loops and those situations with multiple break points. So by me this syntax would definitely achieve an extra +. There were alternative suggestions, e.g. introducing a new keyword "loop", but obviously a new keyword is much harder to do now. I don't know what to add to this actually, I think the idea is understood. I see, it is not the most important problem, but if there is nothing serious against this, I think it's worth it and would be quite a positive (small) improvement and a nice gift to those involved in algorithms. 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 std lib, so the numbers exceeded my expectation (because I expected that it's used mostly in algorithms). Mikhail

On Tue, Sep 25, 2018 at 8:46 PM Mikhail V <mikhailwas@gmail.com> wrote:
I prefer the explicit phrase, ``while True:``. Saying "while" without a condition is strange, like a sentence fragment. The ``while 1:`` pattern is a carryover from Python 2, when ``True`` was not yet a keyword.

On Wed, Sep 26, 2018 at 12:35 PM Michael Selik <mike@selik.org> 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

On Wed, Sep 26, 2018 at 5:38 AM Chris Angelico <rosuav@gmail.com> wrote:
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.

Le 26/09/2018 à 05:36, Chris Angelico a écrit : 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

On Tue, Sep 25, 2018 at 8:47 PM Mikhail V <mikhailwas@gmail.com> wrote:
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. 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. Python has shown a willingness to introduce new keywords: "async" and "await" were added in Python 3.5, which was released about 3 years ago. I imagine that a new loop keyword could be introduced in a backwards-compatible way. If you dislike the look of `while True:`, you can almost always hide it inside a generator and use a `for … in …` block with clearer meaning.

On Wed, Sep 26, 2018 at 4:46 PM Mark E. Haase <mehaase@gmail.com> wrote:
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

Mikhail V writes:
AIUI, the situation is this: (Executive summary) The restriction was imposed by Guido as a design feature. (Excruciating detail) There are three general possibilities: 1. The syntax recognizes some tokens as significant, but they are not reserved at all. It's arguable that Lisp works this way (some will argue that only parentheses create syntax in Lisp, but I don't think humans perceive it that way). 2. The syntax recognizes keywords (ie, reserved for their syntactic meaning) in a context-dependent fashion. In Python, an example might be treating break and continue this way: inside a loop they are reserved, but you can use them as identifiers outside of a loop. I believe that the async-related keywords were treated this way at first. 3. Syntactically-significant keywords are reserved for their syntactic purpose globally. This is the rule in Python. Guido chose #3 as a matter of designing Python. IIRC, the logic was that #1 admits awful unreadable code like "if if then then else else" and nobody needs to write that, and it's hard to write a grammar for it that allows generation of good error messages. The rules for #2, being context-dependent, are unclear to most humans and you don't get a significant benefit for many keywords (consider how useful "break" would be as a variable if you couldn't reference it in a loop!) Besides not having those disadvantages, #3 has the advantage of a simpler grammar and more transparent syntax error reporting, at the minor cost of reserving a very few tokens globally. This does get annoying sometimes (you see a lot of use of 'class_' and 'klass' in some contexts), but I hardly ever notice it. For me at least, shadowing builtins is a much more frequent problem/annoyance.

I put the list of related discussion here, just in case. Same suggestion: https://mail.python.org/pipermail/python-dev/2005-July/054914.html Idea for the "loop" keyword: https://mail.python.org/pipermail/python-ideas/2014-June/028202.html (followed by the same suggestion from @Random832: https://mail.python.org/pipermail/python-ideas/2014-June/028220.html) Somewhat related PEP (rejected) + discussion links inside: https://legacy.python.org/dev/peps/pep-0315/ (I've meditated a bit on this - but could not get what was actually the point of that idea.) Plus some other related discussions, maybe not directly related, but something along the lines might be interesting. An old and lively one: https://mail.python.org/pipermail/python-list/1999-April/002557.html https://mail.python.org/pipermail/python-list/1999-May/008535.html Another one: https://mail.python.org/pipermail/python-list/2000-December/029972.html ( Particularly this: https://mail.python.org/pipermail/python-list/2000-December/052694.html ) Yet another "1" vs "True" debate: https://mail.python.org/pipermail/python-list/2012-January/618649.html New era: https://mail.python.org/pipermail/python-list/2017-April/721182.html

Mikhail V has suggested "while:" as a shorthand for "while True:". He's also provided a helpful list of URLs for related discussion. I'd like to suggest another approach. My suggestion is to improved documentation and help. For me a search for python "while True" produces as the top result https://wiki.python.org/moin/WhileLoop. Please take a look at this page. I don't think it's suitable for novices. In particular, about 25% of the page compares "while 1:" to "while True:" and changes between Python2 and Python3. Searching for site:docs.python.org "while True" produces as top result https://docs.python.org/3/tutorial/controlflow.html. Please also look at this page. It has two examples of "while True:", but does not say that "while True:" means loop forever (unless there is a break or an exception). Today I saw https://blog.codeclub.org.uk/2018/06/13/how-to-move-from-scratch-to-python-i... On this page, a Code Club volunteer gives as debugging advice <quote> I use comparisons of Scratch to Python at the beginning, for example to explain that ‘while True’ is the same as the ‘Forever’ block. I also have some Python cheat sheets which give examples of common Python code. Some of the learners find it easier to debug Python than Scratch, because Python gives you error messages to indicate what went wrong. It’s usually down to spelling errors! </quote> My final URL is https://www.youtube.com/watch?v=QEJ8tE9lAeE. This is a 3 minute video: Python For Beginners - How to use a While True Loop in Python. Something that can be done now, which can soon bring benefit to many, is to find or write material that will help novices master Python idioms such as "while True:". I'm sure the Code Club would like to put such material on its web site. -- Jonathan On Sat, Sep 29, 2018 at 4:58 AM Mikhail V <mikhailwas@gmail.com> wrote:

On 02/10/18 12:31, Jonathan Fine wrote:
Before we hare off into the middle distance, could you show that there is a problem that needs to be addressed here? I killfiled Mikhail a long time ago, so I haven't actually seen any justification here beyond "I don't want to have to type five more characters."
Is it intended solely for novices?
This is an invidious example. "while" is actually introduced on the previous page, as the first paragraph says. Expecting a statement of the blindingly obvious here is inappropriate, surely. (And yes, my experience with early teens is that it *is* blindingly obvious.)
Um, yay? I'm not sure what your point here is. So Scratch has a Forever block, so what?
I'm sorry, but I find the idea that idioms like "while True:" need to be *mastered* a tad patronising. I have genuinely not had to take more than five minutes to get it through to an uninterested and not particularly technical 12-year old. -- Rhodri James *-* Kynesim Ltd

On Tue, Sep 25, 2018 at 8:46 PM Mikhail V <mikhailwas@gmail.com> wrote:
I prefer the explicit phrase, ``while True:``. Saying "while" without a condition is strange, like a sentence fragment. The ``while 1:`` pattern is a carryover from Python 2, when ``True`` was not yet a keyword.

On Wed, Sep 26, 2018 at 12:35 PM Michael Selik <mike@selik.org> 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

On Wed, Sep 26, 2018 at 5:38 AM Chris Angelico <rosuav@gmail.com> wrote:
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.

Le 26/09/2018 à 05:36, Chris Angelico a écrit : 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

On Tue, Sep 25, 2018 at 8:47 PM Mikhail V <mikhailwas@gmail.com> wrote:
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. 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. Python has shown a willingness to introduce new keywords: "async" and "await" were added in Python 3.5, which was released about 3 years ago. I imagine that a new loop keyword could be introduced in a backwards-compatible way. If you dislike the look of `while True:`, you can almost always hide it inside a generator and use a `for … in …` block with clearer meaning.

On Wed, Sep 26, 2018 at 4:46 PM Mark E. Haase <mehaase@gmail.com> wrote:
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

Mikhail V writes:
AIUI, the situation is this: (Executive summary) The restriction was imposed by Guido as a design feature. (Excruciating detail) There are three general possibilities: 1. The syntax recognizes some tokens as significant, but they are not reserved at all. It's arguable that Lisp works this way (some will argue that only parentheses create syntax in Lisp, but I don't think humans perceive it that way). 2. The syntax recognizes keywords (ie, reserved for their syntactic meaning) in a context-dependent fashion. In Python, an example might be treating break and continue this way: inside a loop they are reserved, but you can use them as identifiers outside of a loop. I believe that the async-related keywords were treated this way at first. 3. Syntactically-significant keywords are reserved for their syntactic purpose globally. This is the rule in Python. Guido chose #3 as a matter of designing Python. IIRC, the logic was that #1 admits awful unreadable code like "if if then then else else" and nobody needs to write that, and it's hard to write a grammar for it that allows generation of good error messages. The rules for #2, being context-dependent, are unclear to most humans and you don't get a significant benefit for many keywords (consider how useful "break" would be as a variable if you couldn't reference it in a loop!) Besides not having those disadvantages, #3 has the advantage of a simpler grammar and more transparent syntax error reporting, at the minor cost of reserving a very few tokens globally. This does get annoying sometimes (you see a lot of use of 'class_' and 'klass' in some contexts), but I hardly ever notice it. For me at least, shadowing builtins is a much more frequent problem/annoyance.

I put the list of related discussion here, just in case. Same suggestion: https://mail.python.org/pipermail/python-dev/2005-July/054914.html Idea for the "loop" keyword: https://mail.python.org/pipermail/python-ideas/2014-June/028202.html (followed by the same suggestion from @Random832: https://mail.python.org/pipermail/python-ideas/2014-June/028220.html) Somewhat related PEP (rejected) + discussion links inside: https://legacy.python.org/dev/peps/pep-0315/ (I've meditated a bit on this - but could not get what was actually the point of that idea.) Plus some other related discussions, maybe not directly related, but something along the lines might be interesting. An old and lively one: https://mail.python.org/pipermail/python-list/1999-April/002557.html https://mail.python.org/pipermail/python-list/1999-May/008535.html Another one: https://mail.python.org/pipermail/python-list/2000-December/029972.html ( Particularly this: https://mail.python.org/pipermail/python-list/2000-December/052694.html ) Yet another "1" vs "True" debate: https://mail.python.org/pipermail/python-list/2012-January/618649.html New era: https://mail.python.org/pipermail/python-list/2017-April/721182.html

Mikhail V has suggested "while:" as a shorthand for "while True:". He's also provided a helpful list of URLs for related discussion. I'd like to suggest another approach. My suggestion is to improved documentation and help. For me a search for python "while True" produces as the top result https://wiki.python.org/moin/WhileLoop. Please take a look at this page. I don't think it's suitable for novices. In particular, about 25% of the page compares "while 1:" to "while True:" and changes between Python2 and Python3. Searching for site:docs.python.org "while True" produces as top result https://docs.python.org/3/tutorial/controlflow.html. Please also look at this page. It has two examples of "while True:", but does not say that "while True:" means loop forever (unless there is a break or an exception). Today I saw https://blog.codeclub.org.uk/2018/06/13/how-to-move-from-scratch-to-python-i... On this page, a Code Club volunteer gives as debugging advice <quote> I use comparisons of Scratch to Python at the beginning, for example to explain that ‘while True’ is the same as the ‘Forever’ block. I also have some Python cheat sheets which give examples of common Python code. Some of the learners find it easier to debug Python than Scratch, because Python gives you error messages to indicate what went wrong. It’s usually down to spelling errors! </quote> My final URL is https://www.youtube.com/watch?v=QEJ8tE9lAeE. This is a 3 minute video: Python For Beginners - How to use a While True Loop in Python. Something that can be done now, which can soon bring benefit to many, is to find or write material that will help novices master Python idioms such as "while True:". I'm sure the Code Club would like to put such material on its web site. -- Jonathan On Sat, Sep 29, 2018 at 4:58 AM Mikhail V <mikhailwas@gmail.com> wrote:

On 02/10/18 12:31, Jonathan Fine wrote:
Before we hare off into the middle distance, could you show that there is a problem that needs to be addressed here? I killfiled Mikhail a long time ago, so I haven't actually seen any justification here beyond "I don't want to have to type five more characters."
Is it intended solely for novices?
This is an invidious example. "while" is actually introduced on the previous page, as the first paragraph says. Expecting a statement of the blindingly obvious here is inappropriate, surely. (And yes, my experience with early teens is that it *is* blindingly obvious.)
Um, yay? I'm not sure what your point here is. So Scratch has a Forever block, so what?
I'm sorry, but I find the idea that idioms like "while True:" need to be *mastered* a tad patronising. I have genuinely not had to take more than five minutes to get it through to an uninterested and not particularly technical 12-year old. -- Rhodri James *-* Kynesim Ltd
participants (9)
-
Brice Parent
-
Chris Angelico
-
Jonathan Fine
-
Mark E. Haase
-
Michael Selik
-
Mike Miller
-
Mikhail V
-
Rhodri James
-
Stephen J. Turnbull