
On Sun, Dec 15, 2019, 2:21 PM Christopher Barker <pythonchb@gmail.com> wrote:
On Sun, Dec 15, 2019 at 6:40 AM David Mertz <mertz@gnosis.cx> wrote:
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.
How file-like do you need? I've certainly written things that usually take an actual file, but sometimes get io.StringIO, or an SQL cursor. On the other hand, I doubt I'd ever have the same handling of anything coming from itertools.permutations(). 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....
I'm 20 years with Python too. But one habit I've changed is to use next() rather than .readline() in this sort of code. Even if my current need is for a file-as-such, there's no reason to restrict that. On the other hand, I use .readlines() and .read() plenty often enough. Sometimes greedy and concrete is appropriate.