[Python-ideas] Deterministic iterator cleanup

Todd toddrjen at gmail.com
Wed Oct 19 11:07:16 EDT 2016


On Wed, Oct 19, 2016 at 3:38 AM, Neil Girdhar <mistersheik at gmail.com> wrote:

> This is a very interesting proposal.  I just wanted to share something I
> found in my quick search:
>
> http://stackoverflow.com/questions/14797930/python-
> custom-iterator-close-a-file-on-stopiteration
>
> Could you explain why the accepted answer there doesn't address this issue?
>
> class Parse(object):
>     """A generator that iterates through a file"""
>     def __init__(self, path):
>         self.path = path
>
>   def __iter__(self):
>         with open(self.path) as f:
>             yield from f
>
>
> Best,
>
> Neil
>
>
I think the difference is that this new approach guarantees cleanup the
exact moment the loop ends, no matter how it ends.

If I understand correctly, your approach will do cleanup when the loop ends
only if the iterator is exhausted.  But if someone zips it with a shorter
iterator, uses itertools.islice or something similar, breaks the loop,
returns inside the loop, or in some other way ends the loop before the
iterator is exhausted, the cleanup won't happen when the iterator is
garbage collected.  And for non-reference-counting python implementations,
when this happens is completely unpredictable.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20161019/a6819766/attachment.html>


More information about the Python-ideas mailing list