do...while

Andrae Muys amuys at shortech.com.au
Fri Jun 21 02:22:54 CEST 2002


"Sean 'Shaleh' Perry" <shalehperry at attbi.com> wrote in message news:<mailman.1024595068.19382.python-list at python.org>...
> On 20-Jun-2002 Michael P. Soulier wrote:
> >     Greetings.
> > 
> >     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. 
> > 
> > ie.
> > 
> >     line = filehandle.readline()
> >     while len(line) > 5:
> >         line = filehandle.readline()
> > 
> >     A do...while here would be nice. 
> > 
> >     Just curious. 
> > 
> 
> while 1:
>     line = filehandle.readline()
>     if len(line) <= 5: break
>     ....
>     ....
> 
> is a typical way to implement it.

or if you prefer to avoid the implied infinate loop...

for line in filehandle:
    if len(line) <= 5: break
    ....

I haven't tested for any interaction with this and any
read-ahead-buffering, however the documentation defines the behaviour
as being repeated calls to readline() so it should work fine.

Andrae Muys



More information about the Python-list mailing list