The rap against "while True:" loops

Hendrik van Rooyen hendrik at
Sun Oct 11 08:42:53 CEST 2009

On Saturday, 10 October 2009 22:15:21 kj wrote:
> I'm coaching a group of biologists on basic Python scripting.  One
> of my charges mentioned that he had come across the advice never
> to use loops beginning with "while True".  Of course, that's one
> way to start an infinite loop, but this seems hardly a sufficient
> reason to avoid the construct altogether, as long as one includes
> an exit that is always reached.  (Actually, come to think of it,
> there are many situations in which a bona fide infinite loops
> (typically within a try: block) is the required construct, e.g.
> when implementing an event loop.)
> I use "while True"-loops often, and intend to continue doing this
> "while True", but I'm curious to know: how widespread is the
> injunction against such loops?  Has it reached the status of "best
> practice"?

Others have given various valid answers, but I have not seen this one:

It is often necessary, in long running applications, to set up loops that you 
would really like to run until the end of time. - the equivalent of a "serve 
forever" construct.  Then while True is the obvious way to spell it.

- Hendrik

More information about the Python-list mailing list