[Python-Dev] Single- vs. Multi-pass iterability

Alex Martelli aleax@aleax.it
Thu, 11 Jul 2002 13:09:14 +0200


On Thursday 11 July 2002 12:47 pm, Guido van Rossum wrote:
> > > Proposal: make files really support the iterator
> > > protocol. __iter__ would return self and next() would call
> > > readline and raise StopIteration if ''.  If anyone wants the
> > > xreadline performance improvement it should be explicit.
>
> No.  I won't have "for line in file" be slower than attainable.

+1.  I _intensely_ want to be able to teach beginners to use "for line in 
file" and have it be fast in the common case.  "Nice" behavior for rarer 
cases of prematurely interrupted loops is OK, if feasible, but secondary.  
Having "for line in file" play nicely with other method calls on 'file' has 
no importance to me in this context -- no more than, e.g., having "for item 
in alist" play nicely with calls to mutating methods of object alist.


> The only solution I accept is a complete rewrite of the I/O system
> without using stdio, so xreadlines can be integrated.

I thought Just's suggestion (about having the file object remember
the xreadlines object in use, so that another for loop would continue
right where the first one exited) seemed like a reasonable hack -- a
compromise of reasonably little effort for some small secondary gain.

Guess I must be missing something?  Of course the "complete rewrite"
is an alluring prospect -- for many other reasons, such as enabling
user control of file buffering in cross-platform ways, *yum* -- but it's not 
going to happen in time for 2.3 anyway, is it?


Alex