PEP 315: Enhanced While Loop

Alex Martelli aleax at aleax.it
Mon May 5 11:55:39 EDT 2003


Andrew Koenig wrote:
   ...
> clearer and requires no new keywords:
> 
>         while:
>             <setup code>
>         and while <condition>:
>             <loop body>

Lovely.  Best syntax I've seen so far.

> Indeed, we can think of this usage as syntactic sugar for the following:
> 
>     while:   ==>   while True:
> 
>     <dedent> and while <condition>: <indent>   ==>   if not (<condition>):
>     break

OK.  Makes the semantic of an else clause somewhat peculiar (the else
clause's body executes if the condition on the FIRST while causes the
exist, but not if the loop exists because of the condition on any of
the FOLLOWING while's) but perhaps that's secondary.

> By implication, there is no obvious reason that one could not write
> 
>         while <condition1>:
>             <code1>
>         and while <condition2>:
>             <code2>
>         and while <condition3>:
>             <code3>

Yes, this generality would help.

> which would be equivalent to
> 
>         while <condition1>:
>             <code1>
>             if not (<condition2>): break
>             <code2>
>             if not (<condition3>): break
>             <code3>
> 
> Looking at these examples, I find myself liking the "and while" idea more,
> because it uses (negative) indentation to mark the possible exit points.

I do agree the outdents make the structure clearer -- indeed, that's
essentially THE point.  I think this proposal should go in the PEP --
it does not suffer disadvantages such as new keyword introduction.


Alex





More information about the Python-list mailing list