Warning about "for line in file:"

Michael Hudson mwh at python.net
Thu Feb 21 10:04:04 EST 2002


Aldo Cortesi <aldo at nullcube.com> writes:

> Thus spake Neil Schemenauer (nas at python.ca):
> 
> > Jason Orendorff wrote:
> > > I agree!  This could be improved by having
> > > file.__iter__() always return the same object.  But that
> > > doesn't solve the problem in full.
> > 
> > Perhaps the buffer should be attached to the file object
> > itself instead of to the iterator.  read() and readline()
> > could then be changed to use the buffer if it exists.

Yes.

> ... but this would leave a discrepancy between the actual
> file position and the notional "read" position. Among other
> things, file.tell() would then give the wrong information,
> and file.write() would write to an unexpected location. 

I think if you interleave reads and writes without an intervening
seek(), you lose anyway.

Cheers,
M.

-- 
  > Emacs is a fashion statement.
  No, Gnus is a fashion statement.  Emacs is clothing.  Everyone 
  else is running around naked.
        -- Karl Kleinpaste & Jonadab the Unsightly One, gnu.emacs.gnus



More information about the Python-list mailing list