[Python-ideas] Deterministic iterator cleanup

Ethan Furman ethan at stoneleaf.us
Fri Oct 21 23:22:04 EDT 2016


On 10/21/2016 03:48 PM, Amit Green wrote:

> NOTE: This is my first post to this mailing list, I'm not really sure
>        how to post a message, so I'm attempting a reply-all.

Seems to have worked! :)

> I like Nathaniel's idea for __iterclose__.
>
> I suggest the following changes to deal with a few of the complex issues
> he discussed.

Your examples are interesting, but they don't seem to address the issue of closing down for loops that are using generators when those loops exit early:

-----------------------------
def some_work():
     with some_resource():
       for widget in resource:
           yield widget


for pane in some_work():
     break:

# what happens here?
-----------------------------

How does your solution deal with that situation?  Or are you saying that this would be closed with your modifications, and if I didn't want the generator to be closed I would have to do:

-----------------------------
with some_work() as temp_gen:
     for pane in temp_gen:
         break:

     for another_pane in temp_gen:
         # temp_gen is still alive here
-----------------------------

In other words, instead using the preserve() function, we would use a with statement?

--
~Ethan~


More information about the Python-ideas mailing list