[Python-ideas] Deterministic iterator cleanup

Yury Selivanov yselivanov.ml at gmail.com
Tue Oct 25 11:59:29 EDT 2016



On 2016-10-25 4:33 AM, Nick Coghlan wrote:
> I'm starting to think that we instead need a way
> to let them easily say "This resource, the one I just created or have
> otherwise gained access to? Link its management to the lifecycle of
> the currently running function or frame, so it gets cleaned up when it
> finishes running".


But how would it help with a partial iteration over generators
with a "with" statement inside?

    def it():
        with open(file) as f:
            for line in f:
                yield line

Nathaniel proposal addresses this by fixing "for" statements,
so that the outer loop that iterates over "it" would close
the generator once the iteration is stopped.

With your proposal you want to attach the opened file to the
frame, but you'd need to attach it to the frame of *caller* of
"it", right?

Yury


More information about the Python-ideas mailing list