News for hackers. Stuff that matters.
Re:Specific ways to write better Python (2017) by ehsankia on Saturday June 20, 2020 @08:23PM (#23588617)
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)
Re:Specific ways to write better Python (2017) by carapace on Saturday June 20, 2020 @10:38PM (#23589293)
Oh hey, that's a little nicer, isn't it? Cheers