On Wed, Oct 19, 2016 at 11:08 AM Todd <toddrjen@gmail.com> wrote:
On Wed, Oct 19, 2016 at 3:38 AM, Neil Girdhar <mistersheik@gmail.com> wrote:
This is a very interesting proposal.  I just wanted to share something I found in my quick search:


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@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
_______________________________________________
Python-ideas mailing list
Python-ideas@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@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.