On 12/15/2019 2:21 PM, Christopher Barker wrote:
On Sun, Dec 15, 2019 at 6:40 AM David Mertz <[email protected]
<mailto:[email protected]>> 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...
I do this a lot for test cases. Instead of having a test file, I just
have a list of lines that would be in the file, and pass that list in to
a function that just takes an iterator. The function normally is passed
a file, but also works for my test code which doesn't use files.
Eric
_______________________________________________
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/JKNOKR5IC2DQSHMNIJW3NTKP4QGNJNEP/
Code of Conduct: http://python.org/psf/codeofconduct/