Newbie question about file input
Ben Last
ben at benlast.com
Mon Aug 16 12:54:17 EDT 2004
> Isn't it a strange way to do a loop? Create a loop with a foo
> condition and use an if to break it?
Not necessarily. The use of "while True" at least signals that the loop
exit condition isn't at the top. I suspect, though, that this is a "style"
question, and as such there will be a Mighty Discussion about it...
> =====
> line=zf.readline()
> while line:
> . . .
> line=zf.readline()
> =====
> Isn't this code much more easy to understand?
Arguably so, but you do the "next loop statement" (in this case,
"line=zf.readline()") in two places. Redundancy. Chance of changing one
and forgetting to change the other. Eww.
But:
for line in zf.readlines():
is even neater
humbly yours,
ben
More information about the Python-list
mailing list