PEP 315: Enhanced While Loop

SUZUKI Hisao suzuki611 at oki.com
Wed May 7 05:32:34 EDT 2003


In <mailman.1052166437.3263.python-list at python.org>, Steven Taschuk wrote:
[...]
> Of course, with 'while True' and 'if ...: break', usually the loop
> is not infinite (barring bugs).  If I understand correctly, you're
> saying that 'while True' suggests that the programmer *intends* an
> infinite loop, and so is misleading.
>
> If that's right, how about this?
>
>     loop:
>         do some stuff
>         if should stop now:
>             break
>         do some other stuff
>
> The 'loop' just says "this is a loop; execution comes back here at
> the end of the block".  Unlike 'while True', it does not also
> explicitly say "we will continue looping as long as True is true".
> (The notion is that the problem is while's mandatory condition,
> which adds no information in the case of these so-called infinite
> loops.)
>
> Any better in your view?  (I can't tell; I'm too used to looking
> for 'if ...: break' after seeing 'while True'.)
[...]
> Imho 'break' and 'continue' together make 'while' a "loop
> construct of sufficient power and flexibility".  :)

I think so, too.
I wish Python kept clean without overdone constructs...  I think it
is better only to add an intuitive keyword "loop" which is equivalent
to "while True" (like several other languages) if we need to fix the
language.

And from the mathematical view, "while...else..." is cleanly
defined.  Let W be "while e: s else: t".
Then W is the least fixed point of the equation:
   W = if e:
         s; W
       else:
         t

Imho, both constructs of W. I. Carroll and A. Koenig lack
such simplicity.

-- SUZUKI Hisao






More information about the Python-list mailing list