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

Guido van Rossum guido@python.org
Tue, 16 Jul 2002 08:52:56 -0400


> On the new version of patch #580331 the cache is invalidated on a seek. 
> 
> > Maybe the xreadlines object could grow a flush() method that throws
> > away its buffer, and f.seek() could call that if there's a cached
> > xreadlines iterator?
> 
> The behavior of an xreadlines object is already undefined after a seek on 
> the file.  This patch doesn't try to fix that.  The invalidation makes sure 
> that the next iter() call will produce a fresh xreadlines, though.

OK, good enough.

> Flushing would be too much work for this little hack. The right solution 
> would be to fully integrate buffering into the file object and get rid of 
> the dependency on the xreadlines module. The xreadlines method will then be 
> equivalent to __iter__ (i.e. return self).  I assume that after this rewrite
> the xreadlines module would be deprecated.

Yes, that's what I've called rewriting the I/O system. :-)

> > > See my previous postings for why I think a file should be an iterator.
> > 
> > Haven't seen them but I would agree that this makes sense.
> 
> For some reason I got the impression that you disagreed.

I disagreed with making next simply point to readline, because that
would defeat the speedup we get from using the file iterator.  The
solution in your patch doesn't have this problem.  (Though one *could*
argue that making the file object its own iterator is only confusing;
given that I'm also not sure what problem it solves, I'm at best +0 on
it.)

--Guido van Rossum (home page: http://www.python.org/~guido/)