Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

do...while

2 views
Skip to first unread message

Michael P. Soulier

unread,
Jun 20, 2002, 12:26:35 PM6/20/02
to
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.

Mike

--
Michael P. Soulier, QX41, SKY Tel: 613-765-4699 (ESN: 39-54699)
Optical Networks, Nortel Networks, SDE Pegasus
"...the word HACK is used as a verb to indicate a massive amount
of nerd-like effort." -Harley Hahn, A Student's Guide to Unix

Fredrik Lundh

unread,
Jun 20, 2002, 1:48:20 PM6/20/02
to
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:

while 1:
do stuff
if condition:
break
do stuff

</F>


Sean 'Shaleh' Perry

unread,
Jun 20, 2002, 1:42:56 PM6/20/02
to

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.


Sean 'Shaleh' Perry

unread,
Jun 20, 2002, 2:16:03 PM6/20/02
to
>
> Right, I've used that in the past. It doesn't seem nearly as clean
> though,
> as making the conditions for terminating the loop plain in the loop
> declaration. Considering the Python community's leaning towards readable
> code,
> necessitating a break for code factoring goes against that philosophy.
>

not every language has had a do .. while construct. You code with the tools
given.


Michael P. Soulier

unread,
Jun 20, 2002, 2:12:31 PM6/20/02
to
On Thu, Jun 20, 2002 at 10:42:56AM -0700, Sean 'Shaleh' Perry wrote:
>
> 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.

Right, I've used that in the past. It doesn't seem nearly as clean though,


as making the conditions for terminating the loop plain in the loop
declaration. Considering the Python community's leaning towards readable code,
necessitating a break for code factoring goes against that philosophy.

Mike

Fredrik Lundh

unread,
Jun 20, 2002, 3:06:17 PM6/20/02
to
Michael P. Soulier wrote:

> Right, I've used that in the past. It doesn't seem nearly as clean though,
> as making the conditions for terminating the loop plain in the loop
> declaration.

this has been debated hundreds of times (google can show you all
arguments for and against adding yet another control structure).

unless you really don't have something better to do with your time,
I suggest you get over it and write some code...

</F>


Michael P. Soulier

unread,
Jun 20, 2002, 2:53:18 PM6/20/02
to
On Thu, Jun 20, 2002 at 11:16:03AM -0700, Sean 'Shaleh' Perry wrote:
>
> not every language has had a do .. while construct. You code with the tools
> given.

If we always just code with the tools given, Python would never have been
born.

Cheers,

François Pinard

unread,
Jun 20, 2002, 2:56:34 PM6/20/02
to
[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


Chris Barker

unread,
Jun 20, 2002, 3:30:47 PM6/20/02
to

"Michael P. Soulier" wrote:

> How do you people handle the lack of a do...while in Python?

> line = filehandle.readline()


> while len(line) > 5:
> line = filehandle.readline()

The most common idiom is:

while 1:
line = filehandle.readline()

if not len(line > 5):
break
do stuff....

I try to avoid "break" but is it is at the top of the loop, the meaning
is pretty clear.

-Chris

--
Christopher Barker, Ph.D.
Oceanographer

NOAA/OR&R/HAZMAT (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris....@noaa.gov

Bjorn Pettersen

unread,
Jun 20, 2002, 6:57:21 PM6/20/02
to
> From: Michael P. Soulier [mailto:msou...@nortelnetworks.com]
>
> On Thu, Jun 20, 2002 at 10:42:56AM -0700, Sean 'Shaleh' Perry wrote:
> >
> > 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.
>
> Right, I've used that in the past. It doesn't seem nearly
> as clean though, as making the conditions for terminating the
> loop plain in the loop declaration. Considering the Python
> community's leaning towards readable code, necessitating a
> break for code factoring goes against that philosophy.

Doing a quick search over our C++ libraries (~900KLoc), "do {} while()"
seems to be used in right around 3% of the loops (including while, for;
excluding recursion).

Seems like overkill to add special syntax for a feature used so
rarely...

-- bjorn


Aahz

unread,
Jun 20, 2002, 7:24:53 PM6/20/02
to
In article <slrnah40l1....@pmerd071.ca.nortel.com>,

Michael P. Soulier <msou...@nortelnetworks.com_.nospam> wrote:
>
> line = filehandle.readline()
> while len(line) > 5:
> line = filehandle.readline()

Use Python 2.2:

for line in filehandle:


if len(line) <= 5:
break

--
Aahz (aa...@pythoncraft.com) <*> http://www.pythoncraft.com/

Project Vote Smart: http://www.vote-smart.org/

François Pinard

unread,
Jun 20, 2002, 7:53:46 PM6/20/02
to
[Bjorn Pettersen]

> Doing a quick search over our C++ libraries (~900KLoc), "do {} while()"
> seems to be used in right around 3% of the loops (including while, for;
> excluding recursion). Seems like overkill to add special syntax for a
> feature used so rarely...

Some of you might remember C.A.R. Hoare's axiomatisation of Pascal, which
also gives good insight for many other languages. The `do {} while()' was
called `repeat' in Pascal. The axiom for `while' is one of the simplest
of the whole language, much simpler than the axiom for `repeat'.

This taught me that there is a theoretical foundation for the popularity of
the `while' construct. After all, program axiomatisation is nothing more
than the formalisation of programmer's thought when s/he produces a program,
and our intuition naturally drives our thoughts towards simpler concepts.

Andrae Muys

unread,
Jun 20, 2002, 8:22:54 PM6/20/02
to
"Sean 'Shaleh' Perry" <shale...@attbi.com> wrote in message news:<mailman.1024595068...@python.org>...

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

Peter Hansen

unread,
Jun 20, 2002, 9:43:47 PM6/20/02
to
"Michael P. Soulier" wrote:
>
> On Thu, Jun 20, 2002 at 10:42:56AM -0700, Sean 'Shaleh' Perry wrote:
> > while 1:
> > line = filehandle.readline()
> > if len(line) <= 5: break
> > ....
> > ....
> >
> > is a typical way to implement it.
>
> Right, I've used that in the past. It doesn't seem nearly as clean though,
> as making the conditions for terminating the loop plain in the loop
> declaration. Considering the Python community's leaning towards readable code,
> necessitating a break for code factoring goes against that philosophy.

Avoiding duplicated code always trumps an uneasy feeling that
the code might not be as pretty as it could be in another
language or with new language constructs...

Once you get over that uneasy feeling, and implement the loop
that way a few times, and focus on maintainability, you'll
probably stop worrying about it. At least, I did. :)

-Peter

Chris Barker

unread,
Jun 21, 2002, 3:53:02 PM6/21/02
to
Bjorn Pettersen wrote:
> Doing a quick search over our C++ libraries (~900KLoc), "do {} while()"
> seems to be used in right around 3% of the loops (including while, for;
> excluding recursion).

I was surprised that it was used so rarely, but them I realized that in
C/C++, assignment returns a value, so you can do the equivalent of:

while line = file.readline():
....

which is used a lot, but can't be done in Python. That's still not a
reason to add the construct, but it is a commonly occuring structure.

Oleg Broytmann

unread,
Jun 21, 2002, 5:24:52 PM6/21/02
to
On Fri, Jun 21, 2002 at 12:53:02PM -0700, Chris Barker wrote:
> while line = file.readline():

In Python it is spelled

for line in file.xreadlines():
...

Oleg.
--
Oleg Broytmann http://phd.pp.ru/ p...@phd.pp.ru
Programmers don't die, they just GOSUB without RETURN.


Bjorn Pettersen

unread,
Jun 21, 2002, 7:45:26 PM6/21/02
to
> From: Oleg Broytmann [mailto:p...@phd.pp.ru]
>
> On Fri, Jun 21, 2002 at 12:53:02PM -0700, Chris Barker wrote:
> > while line = file.readline():
>
> In Python it is spelled
>
> for line in file.xreadlines():
> ...

Actually, now it's spelled:

for line in file:
...

<wink>

-- bjorn


0 new messages