2 ms·
Why would it be difficult? That's just a lazy sequence or iterator in Python terms. In fact the standard way of writing that in Python is with a function and `y
by jules 15y ago
Why would it be difficult? That's just a lazy sequence or iterator in Python terms. In fact the standard way of writing that in Python is with a function and `yield` or a generator expression, not with a class.
def read_csv(file):
for line in file:
yield parse_csv_line(line)
-or-
def read_csv(file):
return (parse_csv_line(line) for line in file)
-or-
def read_csv(file):
return map(parse_csv_line, file)
-or-
from functools import partial
read_csv = partial(map,parse_csv_line)
None of which involve defining a class.
- pjscott 15y agoI notice that your first and second versions return an iterator, while your third and fourth versions return a list. So, you know, be aware of that. (If you want a version of map that doesn't force its arguments and produce a list, you can use itertools.imap. I wish it were a standard function so I didn't have to import it.)
- masklinn 15y ago> you can use itertools.imap. I wish it were a standard function so I didn't have to import it.) In Python 3 it is (the `map` builtin is lazy, it's essentially `itertools.imap` and the old eager `map` has disappeared)