[Python-ideas] Special keyword denoting an infinite loop

Steven D'Aprano steve at pearwood.info
Sat Jun 28 11:11:12 CEST 2014


On Sat, Jun 28, 2014 at 01:04:24AM -0700, Thomas Allen wrote:
> Rust language defines a special way to make an infinite loop (
> http://doc.rust-lang.org/tutorial.html#loops).

Do they give an explanation for why they use a keyword for such a 
redundant purpose?

 
> I propose adding the same keyword to Python. It will be very useful for
> WSGI servers and will suit as a more convenient replacement for recursion
> (hence Python doesn't do TRE).

I understand that *infinite loops* themselves are useful, and that 
recursion can be replaced by iteration, but how does the "loop" keyword 
solve these issues better than "while True"?


> I personally find it much prettier than *while
> True* or *while 1*. 

If the only advantage of this is that you personally find it prettier, 
then I'm a strong -1 on this suggestion.

* I personally find it less elegant than "while True". "while True"
  tells you explicity what it does: it's a while loop, and it operates 
  while True is true (i.e. forever). "loop" looks like an incomplete 
  line: what sort of loop, while, repeat or for? Loop for how long? It's 
  all implicit.

* It's yet another special keyword to memorise. It doesn't eliminate the 
  need to know "while", or to know "True", and it gives you no extra 
  benefit. It's just completely redundant.


Suppose "loop" becomes a keyword in Python 3.5. That means that every 
existing Python program that uses "loop" as a function or variable 
cannot work in Python 3.5. It also means that any Python 3.5 code that 
uses the "loop" keyword 

Adding new keywords is only done for the most critical reasons, or when 
there is no other good alternative, not just on a whim. There is already 
a perfectly good way to write infinite loops, adding the "loop" keyword 
doesn't add anything to the language, it just breaks working code for 
the sake of a minor, cosmetic change.


> It won't cause any problems with existing programs,
> because *loop* is very rarely used as a variable name.

How do you know it is rare? I've written code where loop is a name:

def main():
    setup()
    loop()



-- 
Steven


More information about the Python-ideas mailing list