The rap against "while True:" loops

Stephen Hansen apt.shansen at gmail.com
Sat Oct 10 16:30:46 EDT 2009


>
> 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
> practice"?
>

Its simply the correct way to spell a do-while or intentionally infinite
loop in Python, always has been.

HTH,

--S
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20091010/a036f38c/attachment-0001.html>


More information about the Python-list mailing list