News for hackers. Stuff that matters.
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.

There are no comments on this comment yet.