The rap against "while True:" loops

greg greg at cosc.canterbury.ac.nz
Mon Oct 12 10:42:23 CEST 2009


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".

It's possible this is something he was told in relation to
another language that has more options.

For example, in C there's for(;;) as an alternative, although
there's not much to choose between them. Also you can often
use an assignment-as-condition trick to squeeze a loop-and-
a-half into a while(), and you have do-while loops for
testing at the end.

Python is much more limited -- anything which isn't a
for has to be a while of some shape, so it's harder to
avoid while True without introducing extra complexities
into the code.

-- 
Greg



More information about the Python-list mailing list