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:
        ...

On Sun, Dec 15, 2019, 5:40 AM Oscar Benjamin <[email protected]>
wrote:

> On Sun, 15 Dec 2019 at 05:54, David Mertz <[email protected]> wrote:
> >
> > A pattern I've written a number of times is roughly:
> >
> > lines = open(fname)
> > header = next(lines)
> > for line in lines:
> >     process (line, header)
> >
> > That's not so artificial, I think. Of course, first() would also work
> here. But I'm not sure it's any particular advantage in this case.
>
> I think you haven't understood the point about wanting a different
> exception instead of StopIteration. This is precisely an example of
> what not to do because it can fail in strange ways if the file is
> empty (see above in this thread or PEP 479).
>
> The simplest way to fix this is with 2-arg next:
>
> with open(fname) as lines:
>     header = next(lines, None)
>     if header is None:
>         # Not sure what you want to do here...
>     for line in lines:
>         process(line, header)
>
> --
> Oscar
> _______________________________________________
> Python-ideas mailing list -- [email protected]
> To unsubscribe send an email to [email protected]
> https://mail.python.org/mailman3/lists/python-ideas.python.org/
> Message archived at
> https://mail.python.org/archives/list/[email protected]/message/6LCVRR6LXJEAV4AECSVKIICHK4VRGANI/
> Code of Conduct: http://python.org/psf/codeofconduct/
>
_______________________________________________
Python-ideas mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/[email protected]/message/PJ5IOWEBGNKK2XEDXMW7VV3SO2NF452J/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to