do...while

François Pinard pinard at iro.umontreal.ca
Thu Jun 20 14:56:34 EDT 2002


[Fredrik Lundh]

> Michael P. Soulier wrote:

> > How do you people handle the lack of a do...while in Python?  I find
> > that at times, I must initialize variables for a loop with identical
> > code to the loop, which feels like a waste to me.

> the standard pydiom is:

[F.P. - I negated `condition', presuming we ponder `do...while(condition)'.]

>     while 1:
>         do stuff
>         if not condition:
>             break
>         do stuff

Given the above, I would much prefer:

    do stuff
    while condition:
        do stuff

However, neither are satisfactory, when `do stuff' is long, and may not
be factored out into a separate `def', because for example it plays with
local variables.  My usual idiom is then:

     loop = True
     while loop:
         do stuff
         loop = condition

-- 
François Pinard   http://www.iro.umontreal.ca/~pinard





More information about the Python-list mailing list