[Python-Dev] Wishlist: dowhile

BJörn Lindqvist bjourne at gmail.com
Mon Jun 13 17:53:52 CEST 2005


> > >>do:
> > >>    <body>
> > >>    until <cond>
> > >>
> > >>But I'm sure that has problems too.
> > >
> [Raymond Hettinger]
> > > That looks nice to me.
> 
> [Nick Coghlan]
> > And this could easily be extended to allow code both before and after
> > the 'until', giving a fully general loop:
> >
> >    do:
> >        <body-1-or-more-times>
> >        until <cond>
> >        <body-0-or-more-times>
> >    else:
> >        <on-natural-loop-exit>
> 
> Which is exactly like PEP 315 except there 'until' must be spelled
> 'while not' and the while is properly indented.
> 
> (I'm still not sure whether BJörn *meant* the 'until' to be indented
> or whether he simply made a mistake; his proposal resembles a Pythonic
> version of Pascal's repeat-until, which would have an unindented
> until-clause.)

Sorry, I should have made that clear. I *meant* for 'until' to be
indented. I envisonaged it to be treatened similar to how the raise,
pass and return statements work. Those statements are indented in a
suite and they always end the current block. Well, technically you can
write stuff like this:

suite:
    <block>
    raise Exception
    <block2>

But 'block2' will always be dead code. Emacs, for example, always
dedents when you write any of those three statements. The reason I
suggested was not so that you could extend it to fully general loops
like Nick suggested (which I can't comment on because I never use
else: in looping constructs and always keep the condition check and
the top or bottom), but because I thought that a dedented 'until'
would be very hard to parse and in general look very unpythonic:

do:
    <block>
until <cond>

Written like this it is not very obvious that the 'unil' is part of
the do-until suite. I also imagine it to be difficult to parse and it
breaks the rule that suites end when there is a dedentation. So, IMHO
using an indented 'until' is the least evil of a number of evils.
 
> Why are you so excited about having until indented? You didn't give
> any examples with multiple occurrences. A single occurrence works just
> fine unindented, as PEP 315 has already shown.
> 
> The indented until sounds like unnecessary syntactic sugar for 'if X:
> break' -- not very Pythonic.

Yes, but grepping the stdlib produces over 300 hits for "while 1:" and
"while True:" combined. Some of those a "if <cond>: break" in the
middle and some would be better written as generators, but lots of
them would be rewritten as do-while's. So I think there is more than
enough use cases for syntactic sugar for do-while loops.

-- 
mvh Björn


More information about the Python-Dev mailing list