On Sun, Dec 15, 2019 at 6:40 AM David Mertz <mertz@gnosis.cx> wrote:
Yes, of course. I was just trying to illustrate using next() in a non-artificial way. In real code (but truthfully, probably not in my quick "one off" scripts) I write

lines = get_lines_file_or_elswhere(resource)
header = next(lines, sentinel)
if looks_like_header(header):
    for line in lines:
        ...

Hmm, interesting -- so this means that you do write code expecting a generic iterator, rather than a file-like object.

I can't say I've ever done that, nor seem anyone else to that.

I'm curious: what other iterators might this code be expected to work with? (that is, a list of lines, as returned by file.readlines() would not work --you'd have to wrap it in iter() first...

But apparently it reflects the move that Python has been making toward being all about iterators.

I think first() would be a help mostly to folks that DON'T think primarily in terms of iterators.  And frankly, I think that is a population Python should strive to support.

For the record, I write a lot of code that looks like:

data_file = get_lines_file_(resource)
header = data_file.readline()
if looks_like_header(header):
    for line in data_file:
        ...

That is, I'm always expecting a file-like object, rather than a generic iterator. That may be because I developed habits long before files were iterators....

BTW: this has been a REALLY LONG thread -- I think it's time for a concrete proposal to be written up, sonce it appears we're not all clear on what we're talking about. For my part I think a first() function would be nice, an am open to a couple variations, so someone with a stronger opinion should propose something.

Tim: what version do you have in mind?

-CHB


--
Christopher Barker, PhD

Python Language Consulting
  - Teaching
  - Scientific Software Development
  - Desktop GUI and Web Development
  - wxPython, numpy, scipy, Cython