File iteration in 2.2
Erik Max Francis
max at alcyone.com
Wed Aug 28 21:32:56 CEST 2002
Clarence Gardner wrote:
> Your argument rests on the assertion "file iterators are really
> intended work like the F.xreadlines() method". Is this true? I
> would hope not. xreadlines was a hack, intended to provide some
> kind of iteration over a file before there was an iteration protocol.
> That to me does not imply that, upon implementing the iteration
> protocol, a file iterator should behave like xreadlines.
But it does behave precisely like .xreadlines (as far as I know). That
is, it buffers the reads in chunks as it goes.
> It is possible to break out of a loop over other kinds of iterators,
> and then continue them:
> Why should a file iterator be different?
You can break out of it just fine. You could even rewind and restart
the iteration from the start if you liked. You just can't expect the
file pointer to be pointing immediately after the last line you
processed because of buffering.
Erik Max Francis / max at alcyone.com / http://www.alcyone.com/max/
__ San Jose, CA, US / 37 20 N 121 53 W / ICQ16063900 / &tSftDotIotE
/ \ There is nothing so subject to the inconstancy of fortune as war.
\__/ Miguel de Cervantes
Church / http://www.alcyone.com/pyos/church/
A lambda calculus explorer in Python.
More information about the Python-list