[Python-ideas] Deterministic iterator cleanup
Neil Girdhar
mistersheik at gmail.com
Wed Oct 19 13:08:24 EDT 2016
On Wed, Oct 19, 2016 at 11:08 AM Todd <toddrjen at gmail.com> wrote:
> 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.
>
> --
>
I don't see that. The "cleanup" will happen when collection is interrupted
by an exception. This has nothing to do with garbage collection either
since the cleanup happens deterministically when the block is ended. If
this is the only example, then I would say this behavior is already
provided and does not need to be added.
>
> ---
> You received this message because you are subscribed to a topic in the
> Google Groups "python-ideas" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/python-ideas/5xdf0WF1WyY/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to
> python-ideas+unsubscribe at googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>
> --
>
> ---
> You received this message because you are subscribed to a topic in the
> Google Groups "python-ideas" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/python-ideas/5xdf0WF1WyY/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to
> python-ideas+unsubscribe at googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20161019/6f1c7206/attachment-0001.html>
More information about the Python-ideas
mailing list