[Python-Dev] PEP 315 - do while

Fuzzyman fuzzyman at voidspace.org.uk
Tue Oct 3 16:28:31 CEST 2006


Nick Coghlan wrote:

> [snip..]
>
>> 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:
>
>   do:
>       pass # Is there an implicit 'while True' at the end of the loop
> body?
>
>   do:
>       while True
>
>   while True:
>       pass
>
Following the current idiom, isn't it more natural to repeat the loop
'until' a condition is met. If we introduced two new keywords, it would
avoid ambiguity in the use of 'while'.

do:
    <loop body>
    until <condition>

A do loop could require an 'until', meaning 'do' is not *just* a
replacement for an infinite loop. (Assuming the parser can be coerced
into co-operation.)

It is obviously still a new construct in terms of Python syntax (not
requiring a colon after '<condition>'.)

I'm sure this has been suggested, but wonder if it has already been
ruled out. An 'else' block could then retain its current meaning
(execute if the loop is not terminated early by an explicit  break.)

Michael Foord
http://www.voidspace.org.uk


More information about the Python-Dev mailing list