while (a=b()) ...

Gordon McMillan gmcm at hypernet.com
Tue May 18 10:48:13 EDT 1999


[Magnus whines]
> >> Well ... IMNSHO, the while 1-idiom is just a symptom of too weak
> >> control structures, or bad structuring of the code <wink>.

[Gordon jokes]
> >Then use a constant in inverse proportion to the endlessness of the
> >loop <wink wink>.

[Blake bites]
> You know, despite your winks, I'm thinking that this is about the
> best and easiest solution I've seen.  Or perhaps you could use
> something like
> 
> while "x (aka somefile.readline) != None":

> (Quick show of hands, how many people are using the construct
>  while "some string": and think that it would break if the string
>  were evaled at some point in the future?  How many of you expect
>  most of your code to break for Python 2.0 anyways?)

Never; don't care; probably not "most", but certainly "some".

When I'm coding a messy loop, I start out with "while 1:", then look 
for any conditions that can be hoisted to the top. But I'm not fond 
of any form of "while" (or "do"), so I also look for ways to do it 
differently. I'll gladly use a couple of "for" loops over one 
"while". They're hard to figure out and error-prone, even without any 
of the proposed synctactic warts, sores and puss-oozing boils.

A quick grep of my Python junk-box directories shows me:

 for:       1141
 while:      223 (including "while 1:")
 while 1:     57

I use "map" more often than "while 1:", and I'm not a functional 
programmer.

"Groundhog Day"-would-never-have-been-filmed-if-they'd-used
 -a-for-loop-instead-of-a-while-ly y'rs

- Gordon




More information about the Python-list mailing list