3 ms·
I'm curious, personally I would just put the file handling inside the function handling the file. Is anything wrong with the following much simpler solution?
by ehsankia 6y ago
I'm curious, personally I would just put the file handling inside the function handling the file. Is anything wrong with the following much simpler solution?
def judgments_from_file(filename):
with open(filename) as f:
for line in f:
yield parse_row(line)
Why wouldn't you let the method that deals with the file also own the file pointer, and then the context manager from the with-statement close it whenever the function is done? Instead of having to do some magical stuff to make sure it closes at the right time? Is it to make unittest/mocking easier?
for judgements in judgments_from_file(filename):
for j in judgements:
process(j)
- carapace 6y agoOh hey, that's a little nicer, isn't it? Cheers