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
the standard pydiom is:
while 1:
do stuff
if condition:
break
do stuff
</F>
while 1:
line = filehandle.readline()
if len(line) <= 5: break
....
....
is a typical way to implement it.
not every language has had a do .. while construct. You code with the tools
given.
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
> 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>
If we always just code with the tools given, Python would never have been
born.
Cheers,
> 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
"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
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
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/
> 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.
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
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
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.
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.
Actually, now it's spelled:
for line in file:
...
<wink>
-- bjorn