while (a=b()) ...

Blake Winton bwinton at tor.dhs.org
Tue May 18 00:38:17 EDT 1999


On Sat, 15 May 1999 11:15:38 -0500, Gordon McMillan <gmcm at hypernet.com> wrote:
>Magnus writes:
>> Well ... IMNSHO, the while 1-idiom is just a symptom of too weak
>> control structures, or bad structuring of the code <wink>.
>Then use a constant in inverse proportion to the endlessness of the
>loop <wink wink>.
>
> while 1:    # an endless loop
>    WorkerThread(serversock.accept()).run()
>
> while 2:    # a breakable loop
>    <blah blah>
>    if <cond>:
>      break
>
>preferring-idiotic-idioms-over-sorry-syntax-ly y'rs

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":
  x = somefile.readline()
  if x == None:
    break
  print x

x = 5
while "x != 2":
    x = x-1
    if x == 2:
        break
    print x

At least that way you can tell at the start what the testing is.  You
could use triple-quoted strings if you had a whole lot of conditions.

while """x != 2 AND
         y != 3 AND
         some readline is not None AND
         I dunno, maybe other tests...""":

The one downside is that when the code gets updated, the comment
doesn't.  But maybe that could be "fixed" in a later version of Python.

(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?)

Later,
Blake.





More information about the Python-list mailing list