[Python-Dev] PEP 315 - do while

Nick Coghlan ncoghlan at gmail.com
Tue Oct 3 15:51:22 CEST 2006

Fuzzyman wrote:
> Nick Coghlan wrote:
>> In my example, the 3 sections (<setup code>, <loop body> and <loop completion 
>> code> are all optional. A basic do-while loop would look like this:
>>       do:
>>           <setup code>
>>           while <condition>
>> (That is, <setup code> is still repeated each time around the loop - it's 
>> called that because it is run before the loop evaluated condition is evaluated)
> +1
> This looks good.

I'm pretty sure it was proposed by someone else a long time ago - I was 
surprised to find it wasn't mentioned in PEP 315.

That said, Guido's observation on PEP 315 from earlier this year holds for me too:

  "I kind of like it but it doesn't strike me as super important" [1]

> The current idiom works fine, but looks unnatural :
> while True:
>     if <condition>:
>        break

There's the rationale for the PEP in a whole 5 lines counting whitespace ;)

> Would a 'while' outside of a 'do' block (but without the colon) then be
> a syntax error ?
> 'do:' would just be syntactic sugar for 'while True:' I guess.

That's the slight issue I still have with the idea - you could end up with 
multiple ways of spelling some of the basic loop forms, such as these 3 
flavours of infinite loop:

       pass # Is there an implicit 'while True' at the end of the loop body?

       while True

   while True:

The other issue I have is that I'm not yet 100% certain it's implementable 
with Python's parser and grammar. I *think* changing the definition of the 
while statement from:

while_stmt ::=
               "while" expression ":" suite
                 ["else" ":" suite]

while_stmt ::=
               "while" expression [":" suite
                 ["else" ":" suite]]

And adding a new AST node and a new type of compiler frame block "DO_LOOP" 
would do the trick (the compilation of a while statement without a trailing 
colon would then check that it was in a DO_LOOP block and raise an error if not).



Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia

More information about the Python-Dev mailing list