4 ms·
> Moreover, according to this best practice, it's impossible to write the csv version correctly. No, it's not: def load(filepath): """with a different f
by dragonwriter 6y ago
> Moreover, according to this best practice, it's impossible to write the csv version correctly.
No, it's not:
def load(filepath):
"""with a different file format"""
with open(filepath) as file:
return list(csv.reader(file))
This both uses csv.reader and fulfills the required contract correctly.
Yes, it's tricky to both follow the “assure a resource is closed” pattern and return a lazy iterator over the external resource which you open.
If you are going to lazily iterate over a resource and do the context manager pattern, you have to do the work within the context manager.