The rap against "while True:" loops
boris.arloff at yahoo.com
Sat Oct 10 22:58:09 CEST 2009
I agree there is no rap against "while True"-loops. As an example these are very useful especially when receiving continuous data over a queue, pipe socket, or over any other connection. You set to block, receive data, then process data and finally loop around to wait for next data segment. Of course should protect against problems with try-except wrappers and by handling exit conditions (e.g. "break") when appropriate.
Is the problem with the specific syntax "while True:" or is it with having infinite loop constructs at all?
--- On Sat, 10/10/09, Stephen Hansen <apt.shansen at gmail.com> wrote:
From: Stephen Hansen <apt.shansen at gmail.com>
Subject: Re: The rap against "while True:" loops
To: python-list at python.org
Date: Saturday, October 10, 2009, 8:30 PM
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?
The injunction is nonexistent (save perhaps in people coming from another language who insist that Python just /must/ have a "proper" do-while construct). "while True" with an exit-test at the end is idiomatic, how you spell "do-while" in Python. There's nothing at all wrong with it, and no real Python programmer will ever say don't-do-it.
Okay, some people prefer to spell it 'while 1', but its the same difference.
Yeah, you have to be certain the exit condition is there and properly formed so it can exit (unless you're using a generator which never empties, of course). But you have to make sure you have a proper exit condition on any looping construct anyways.
No idea where your charge came across the advice, but its nonsense.
Has it reached the status of "best
Its simply the correct way to spell a do-while or intentionally infinite loop in Python, always has been.
-----Inline Attachment Follows-----
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Python-list