<div dir="ltr"><br><br><div class="gmail_quote"><div dir="ltr">On Wed, Oct 19, 2016 at 11:08 AM Todd <<a href="mailto:toddrjen@gmail.com">toddrjen@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr" class="gmail_msg"><div class="gmail_extra gmail_msg"><div class="gmail_quote gmail_msg">On Wed, Oct 19, 2016 at 3:38 AM, Neil Girdhar <span dir="ltr" class="gmail_msg"><<a href="mailto:mistersheik@gmail.com" class="gmail_msg" target="_blank">mistersheik@gmail.com</a>></span> wrote:<br class="gmail_msg"><blockquote class="gmail_quote gmail_msg" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr" class="gmail_msg">This is a very interesting proposal. I just wanted to share something I found in my quick search:<div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg"><a href="http://stackoverflow.com/questions/14797930/python-custom-iterator-close-a-file-on-stopiteration" class="gmail_msg" target="_blank">http://stackoverflow.com/questions/14797930/python-custom-iterator-close-a-file-on-stopiteration</a></div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">Could you explain why the accepted answer there doesn't address this issue?</div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg"><pre class="m_-5368906579283321783m_-764863579377063596lang-py m_-5368906579283321783m_-764863579377063596prettyprint m_-5368906579283321783m_-764863579377063596prettyprinted gmail_msg" style="padding:5px;width:auto;max-height:600px;overflow:auto;font-family:Consolas,Menlo,Monaco,"Lucida Console","Liberation Mono","DejaVu Sans Mono","Bitstream Vera Sans Mono","Courier New",monospace,sans-serif;background-color:rgb(239,240,241);color:rgb(57,51,24);word-wrap:normal"><code style="font-family:Consolas,Menlo,Monaco,"Lucida Console","Liberation Mono","DejaVu Sans Mono","Bitstream Vera Sans Mono","Courier New",monospace,sans-serif;white-space:inherit" class="gmail_msg"><span class="m_-5368906579283321783m_-764863579377063596kwd gmail_msg" style="color:rgb(16,16,148)">class</span><span class="m_-5368906579283321783m_-764863579377063596pln gmail_msg" style="color:rgb(48,51,54)"> </span><span class="m_-5368906579283321783m_-764863579377063596typ gmail_msg" style="color:rgb(43,145,175)">Parse</span><span class="m_-5368906579283321783m_-764863579377063596pun gmail_msg" style="color:rgb(48,51,54)">(</span><span class="m_-5368906579283321783m_-764863579377063596pln gmail_msg" style="color:rgb(48,51,54)">object</span><span class="m_-5368906579283321783m_-764863579377063596pun gmail_msg" style="color:rgb(48,51,54)">):</span><span class="m_-5368906579283321783m_-764863579377063596pln gmail_msg" style="color:rgb(48,51,54)">
</span><span class="m_-5368906579283321783m_-764863579377063596str gmail_msg" style="color:rgb(125,39,39)">"""A generator that iterates through a file"""</span><span class="m_-5368906579283321783m_-764863579377063596pln gmail_msg" style="color:rgb(48,51,54)">
</span><span class="m_-5368906579283321783m_-764863579377063596kwd gmail_msg" style="color:rgb(16,16,148)">def</span><span class="m_-5368906579283321783m_-764863579377063596pln gmail_msg" style="color:rgb(48,51,54)"> __init__</span><span class="m_-5368906579283321783m_-764863579377063596pun gmail_msg" style="color:rgb(48,51,54)">(</span><span class="m_-5368906579283321783m_-764863579377063596pln gmail_msg" style="color:rgb(48,51,54)">self</span><span class="m_-5368906579283321783m_-764863579377063596pun gmail_msg" style="color:rgb(48,51,54)">,</span><span class="m_-5368906579283321783m_-764863579377063596pln gmail_msg" style="color:rgb(48,51,54)"> path</span><span class="m_-5368906579283321783m_-764863579377063596pun gmail_msg" style="color:rgb(48,51,54)">):</span><span class="m_-5368906579283321783m_-764863579377063596pln gmail_msg" style="color:rgb(48,51,54)">
self</span><span class="m_-5368906579283321783m_-764863579377063596pun gmail_msg" style="color:rgb(48,51,54)">.</span><span class="m_-5368906579283321783m_-764863579377063596pln gmail_msg" style="color:rgb(48,51,54)">path </span><span class="m_-5368906579283321783m_-764863579377063596pun gmail_msg" style="color:rgb(48,51,54)">=</span><span class="m_-5368906579283321783m_-764863579377063596pln gmail_msg" style="color:rgb(48,51,54)"> path</span></code></pre></div><div class="gmail_msg"><pre class="m_-5368906579283321783m_-764863579377063596lang-py m_-5368906579283321783m_-764863579377063596prettyprint m_-5368906579283321783m_-764863579377063596prettyprinted gmail_msg" style="padding:5px;width:auto;max-height:600px;overflow:auto;font-family:Consolas,Menlo,Monaco,"Lucida Console","Liberation Mono","DejaVu Sans Mono","Bitstream Vera Sans Mono","Courier New",monospace,sans-serif;background-color:rgb(239,240,241);word-wrap:normal"><code style="font-family:Consolas,Menlo,Monaco,"Lucida Console","Liberation Mono","DejaVu Sans Mono","Bitstream Vera Sans Mono","Courier New",monospace,sans-serif;white-space:inherit" class="gmail_msg"><span class="m_-5368906579283321783m_-764863579377063596pln gmail_msg" style="color:rgb(48,51,54)"> </span><span class="m_-5368906579283321783m_-764863579377063596kwd gmail_msg" style="color:rgb(16,16,148)">def</span><span class="m_-5368906579283321783m_-764863579377063596pln gmail_msg" style="color:rgb(48,51,54)"> __iter__</span><span class="m_-5368906579283321783m_-764863579377063596pun gmail_msg" style="color:rgb(48,51,54)">(</span><span class="m_-5368906579283321783m_-764863579377063596pln gmail_msg" style="color:rgb(48,51,54)">self</span><span class="m_-5368906579283321783m_-764863579377063596pun gmail_msg" style="color:rgb(48,51,54)">):</span><span class="m_-5368906579283321783m_-764863579377063596pln gmail_msg" style="color:rgb(48,51,54)">
</span><span class="m_-5368906579283321783m_-764863579377063596kwd gmail_msg" style="color:rgb(16,16,148)">with</span><span class="m_-5368906579283321783m_-764863579377063596pln gmail_msg" style="color:rgb(48,51,54)"> open</span><span class="m_-5368906579283321783m_-764863579377063596pun gmail_msg" style="color:rgb(48,51,54)">(</span><span class="m_-5368906579283321783m_-764863579377063596pln gmail_msg" style="color:rgb(48,51,54)">self</span><span class="m_-5368906579283321783m_-764863579377063596pun gmail_msg" style="color:rgb(48,51,54)">.</span><span class="m_-5368906579283321783m_-764863579377063596pln gmail_msg" style="color:rgb(48,51,54)">path</span><span class="m_-5368906579283321783m_-764863579377063596pun gmail_msg" style="color:rgb(48,51,54)">)</span><span class="m_-5368906579283321783m_-764863579377063596pln gmail_msg" style="color:rgb(48,51,54)"> </span><span class="m_-5368906579283321783m_-764863579377063596kwd gmail_msg" style="color:rgb(16,16,148)">as</span><span class="m_-5368906579283321783m_-764863579377063596pln gmail_msg" style="color:rgb(48,51,54)"> f</span><span class="m_-5368906579283321783m_-764863579377063596pun gmail_msg" style="color:rgb(48,51,54)">:</span><span class="m_-5368906579283321783m_-764863579377063596pln gmail_msg" style="color:rgb(48,51,54)">
</span><span class="m_-5368906579283321783m_-764863579377063596pln gmail_msg"><font color="#101094" class="gmail_msg">yield from f</font></span></code></pre></div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">Best,</div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">Neil<div class="gmail_msg"><div class="m_-5368906579283321783h5 gmail_msg"><br class="gmail_msg"></div></div></div></div></blockquote></div><br class="gmail_msg"></div></div><div dir="ltr" class="gmail_msg"><div class="gmail_extra gmail_msg">I think the difference is that this new approach guarantees cleanup the exact moment the loop ends, no matter how it ends. <br class="gmail_msg"><br class="gmail_msg">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.<br class="gmail_msg"></div></div>
<p class="gmail_msg"></p>
-- <br class="gmail_msg"></blockquote><div><br></div><div>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.</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br class="gmail_msg">
--- <br class="gmail_msg">
You received this message because you are subscribed to a topic in the Google Groups "python-ideas" group.<br class="gmail_msg">
To unsubscribe from this topic, visit <a href="https://groups.google.com/d/topic/python-ideas/5xdf0WF1WyY/unsubscribe" class="gmail_msg" target="_blank">https://groups.google.com/d/topic/python-ideas/5xdf0WF1WyY/unsubscribe</a>.<br class="gmail_msg">
To unsubscribe from this group and all its topics, send an email to <a href="mailto:python-ideas+unsubscribe@googlegroups.com" class="gmail_msg" target="_blank">python-ideas+unsubscribe@googlegroups.com</a>.<br class="gmail_msg">
For more options, visit <a href="https://groups.google.com/d/optout" class="gmail_msg" target="_blank">https://groups.google.com/d/optout</a>.<br class="gmail_msg">
_______________________________________________<br class="gmail_msg">
Python-ideas mailing list<br class="gmail_msg">
<a href="mailto:Python-ideas@python.org" class="gmail_msg" target="_blank">Python-ideas@python.org</a><br class="gmail_msg">
<a href="https://mail.python.org/mailman/listinfo/python-ideas" rel="noreferrer" class="gmail_msg" target="_blank">https://mail.python.org/mailman/listinfo/python-ideas</a><br class="gmail_msg">
Code of Conduct: <a href="http://python.org/psf/codeofconduct/" rel="noreferrer" class="gmail_msg" target="_blank">http://python.org/psf/codeofconduct/</a><br class="gmail_msg">
<br class="gmail_msg">
--<br class="gmail_msg">
<br class="gmail_msg">
---<br class="gmail_msg">
You received this message because you are subscribed to a topic in the Google Groups "python-ideas" group.<br class="gmail_msg">
To unsubscribe from this topic, visit <a href="https://groups.google.com/d/topic/python-ideas/5xdf0WF1WyY/unsubscribe" rel="noreferrer" class="gmail_msg" target="_blank">https://groups.google.com/d/topic/python-ideas/5xdf0WF1WyY/unsubscribe</a>.<br class="gmail_msg">
To unsubscribe from this group and all its topics, send an email to <a href="mailto:python-ideas%2Bunsubscribe@googlegroups.com" class="gmail_msg" target="_blank">python-ideas+unsubscribe@googlegroups.com</a>.<br class="gmail_msg">
For more options, visit <a href="https://groups.google.com/d/optout" rel="noreferrer" class="gmail_msg" target="_blank">https://groups.google.com/d/optout</a>.<br class="gmail_msg">
</blockquote></div></div>