Warning about "for line in file:"

Just van Rossum just at xs4all.nl
Wed Feb 20 21:55:10 CET 2002


In article <mailman.1014235657.26957.python-list at python.org>,
 "Jason Orendorff" <jason at jorendorff.com> wrote:

> > This is highly unintuitive to me, indeed so much so that I would like to 
> > call it a bug!
> 
> I agree! 

Heh, I was starting to think I'm the only one...

> This could be improved by having file.__iter__()
> always return the same object.  But that doesn't solve the problem
> in full.

Still, it would be 100% better than the current situation.

> One possibility is to change file.__iter__() to return
> iter(file.readline, ''), and have users call file.xreadlines()
> explicitly, and accept the semantic oddness, if they need the
> extra speed.  :-\

No, "for line in file" should become *the* idiom, and therefore it 
should also be the fastest idiom.

> Another possible hack would be to have xreadlines.next() call
> file.seek() on the underlying file object, to make sure the two
> are in sync.  That sounds like a pain to me.

I have no idea how expensive seek() is in general, but it would indeed 
clear up the semantics. But what about the other way around? Should a 
seek() also update the iterator? It's just really messy to have these 
two states exist at the same time :-(.

Just



More information about the Python-list mailing list