[Python-Dev] Wishlist: dowhile

Josiah Carlson jcarlson at uci.edu
Wed Jun 15 21:38:16 CEST 2005


Charles Cazabon <python at discworld.dyndns.org> wrote:
> Indeed.  The original poster seems to want something that would work (not
> necessarily look) like this:
> 
>   do:
>       <block>
>   while <condition>
> 
> with <block> executed once prior to <condition> first being tested.  But the
> above is ugly, and you can get much the same effect with Python today:
> 
>   firsttime = True
>   while firsttime or <condition>:
>       <block>
>       firsttime = False
> 
> Seems fairly Pythonic to me.  YMMV.

I agree.  It also handles 'else' clauses very well.  Heck, even Michael
McLay's offering of ...

while True:
    <body-1-or-more-times>
    if <condition>:
        <on-natural-loop-exit>
        break
    <body-0-or-more-times>

... doesn't look too bad, and supports the full range of pre and post
condition code. Just like the indented "until", it may not be obvious
where the breaking condition lies.  However, a syntax highlighting
editor and a properly located comment or two would solve the problem
with "until" and the breaking "if" above.

I'm of the opinion that the "do while" and "loop and a half" variants of
the while loop should be offered as a recipe somewhere in the
documentation.  Perhaps not directly in the tutorial (maybe a link),
describing quickly how you can use while loops to emulate the full
spectrum of looping constructs.

 - Josiah



More information about the Python-Dev mailing list