Graceful handling of first line

Alex Martelli aleaxit at yahoo.com
Sat Oct 9 18:41:37 EDT 2004


Egbert Bouwman <egbert.list at hccnet.nl> wrote:

> On Fri, Oct 08, 2004 at 11:59:32AM +0200, Alex Martelli wrote:
> > 
> > option 3, a bit cutesy:
> > 
> > for first_line in thefile: break
> > for line in thefile: ...
> > 
> > (again, in 2.2 you'll need some foo=iter(thefile)).
> > 
> This technique depends in the file being positioned at line 2,
> after the break.

Not exactly, if by "being positioned" you mean what's normally meant for
file objects (what will thefile.tell() respond, what next five bytes
will thefile.read(5) read, and so on).  All it depends on is the
_iterator_ on the file being "positioned" in the sense in which
iterators are positioned (what item will come if you call next on the
iterator).

In 2.3 a file is-an iterator; in 2.2 you need to explicitly get an
iterator as indicated in the parenthesis you've also quoted.


> However, In the Nutshell book, page 191, you write:
> > Interrupting such a loop prematurely (e.g. with break)
> > leaves the file's current position with an arbitrary value.
> 
> So the information about the current position is useless.
> 
> Do I discover a contradiction ?

Nope -- the file's current position is (e.g.) what tell will respond if
you call it, and that IS arbitrary.  In 2.2 (which is what the Nutshell
covers) you need to explicitly get an iterator to do anything else; in
2.3 you can rely on the fact that a file is its own iterator to make
your code simpler.  But the iteration state is not connected with the
file's current position.


Alex



More information about the Python-list mailing list