Re:Specific ways to write better Python (2017)
by softwaredoug on Saturday June 20, 2020 @09:07PM
(#23588827)
I think that’s also valid...
What you’re describing is more or less what judgments_open does. I just did it with a context manager. But you’re right there are other ways to safely manage the files lifetime.
I also use this pattern for writing judgments, which has work to do when done judgments are all collected and the context exits.
The only reason I can think of to do it the other way is if you want to make unit testing easier, allowing you to pass a fake iterable instead of a file pointer to `judgments_from_file` for testing.