while (a=b()) ...

Stefan Franke spamfranke at bigfoot.de
Fri May 14 11:36:36 EDT 1999


On 14 May 1999 15:48:17 +0200, mlh at idt.ntnu.no (Magnus L. Hetland) wrote:

>spamfranke at bigfoot.de (Stefan Franke) writes:
>
>> I would even go further: The use of "while 1:" as a substitute for other control
>> structures outnumbers its use for endless loops *by far* (at least 20:1 in the
>> standard libs, as a quick grep shows).
>
>What do you mean by "outnumbers its use for endless loops"? It can
>only be used for endless (broken) loops -- because that is what
>"while 1" means, no?

I've used those sloppy phrases to separate "real endless loops" from those
"while 1:" loops that are there to simulate control other structures,

By "real" endless loops I mean those with no intended control flow after the
loop - the program/thread runs inside the loop until its termination. They have
no 'break' or 'raise' inside to continue afterwards..

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

Agreed, but my intention was to turns this into a virtue <0.8 wink [borrowed
from TP]> . We have our structured exits already with

  while 1:
      # some code
      if condition: break
      # some code
      if another_condition: break
     ...

if we were only aquaint people early enough to this idiom. As FAQ 6.30 says, it
appears strange mostly to newbies that bring their expectations from other
languages.
"Real" endless loops usually occur inside servers, events handlers, main
programs, etc only. In existing Python programs, they are a real minority.

Stefan





More information about the Python-list mailing list