On Wed, Oct 19, 2016 at 3:38 AM, Neil Girdhar <mistersh...@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.
_______________________________________________ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/