Re: [Tutor] iterating over a urllib.urlopen().read() object
Magnus Lycka
magnus at thinkware.se
Tue Dec 9 12:42:48 EST 2003
Terry Carroll wrote:
> > I didn't like the "while True:" construct,
But "while True:" with an "if x: break" in it is such a common
Python idiom that you'd better get used to it.
Alan Gauld responded:
> "While" is the correct loop in this case because we don't
> know in advance how many iterations we need to go through.
> Although personally I'd prefer
>
> data = f.read(BUFSIZE)
> while data:
> p.feed(data)
> data = f.read(BUFSIZE)
The disadvantage with this is obviously that it breaks the
"Do It Only Once" principle. Of course, in this tiny code
it doesn't matter, but in many real time situations it might
be a chunk of lines, of at least a much more complicated
expression, and you might end up modifying it in one place,
and not in the other. :(
This would all be solved if Python had a "repeat - until"
kind of loop, but the problem is that nobody has ever
managed to create a good looking syntax for a loop in
Python with a test in the end.
--
Magnus Lycka, Thinkware AB
Alvans vag 99, SE-907 50 UMEA, SWEDEN
phone: int+46 70 582 80 65, fax: int+46 70 612 80 65
http://www.thinkware.se/ mailto:magnus at thinkware.se
More information about the Tutor
mailing list