News for hackers. Stuff that matters.
Re:Specific ways to write better Python (2017) by softwaredoug on Saturday June 20, 2020 @08:14PM (#23588575)
First one, you’re right that’s a bug. I should fix that in the article (and it’s another way generators and lists aren’t interchangeable)

On saving off the judgements var, I think I disagree that’s as much an issue. “With” is the languages feature explicitly designated for safely managing resources/variable lifetimes. One could save a file off created using with in the same way. The language basically tells you doing that with a var created with “with” is a bad idea. The articles solution makes lifetime of the generator more explicit.

Re:Specific ways to write better Python (2017) by carapace on Sunday June 21, 2020 @12:34PM (#23593049)
> The articles solution makes lifetime of the generator more explicit.

Does it? What's the difference between these two snippets?

    with judgments_open('judgments.txt') as judgments
        gather_features(judgments)

    with open('judgments.txt') as f:
        gather_features(judgments_from_file(f))
You still have to complete all the work within the scope of the context manager either way, right?

The code ehsankia presented combines the generator with a closure that includes the context manager, and automatically carries the scope of the context manager along with it, so that when the generator is exhausted the context manager's scope also ends.