<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Wed, Oct 19, 2016 at 3:38 AM, Neil Girdhar <span dir="ltr"><<a href="mailto:mistersheik@gmail.com" target="_blank">mistersheik@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">This is a very interesting proposal. I just wanted to share something I found in my quick search:<div><br></div><div><a href="http://stackoverflow.com/questions/14797930/python-custom-iterator-close-a-file-on-stopiteration" target="_blank">http://stackoverflow.com/<wbr>questions/14797930/python-<wbr>custom-iterator-close-a-file-<wbr>on-stopiteration</a></div><div><br></div><div>Could you explain why the accepted answer there doesn't address this issue?</div><div><br></div><div><pre class="m_-764863579377063596lang-py m_-764863579377063596prettyprint m_-764863579377063596prettyprinted" 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"><span class="m_-764863579377063596kwd" style="color:rgb(16,16,148)">class</span><span class="m_-764863579377063596pln" style="color:rgb(48,51,54)"> </span><span class="m_-764863579377063596typ" style="color:rgb(43,145,175)">Parse</span><span class="m_-764863579377063596pun" style="color:rgb(48,51,54)">(</span><span class="m_-764863579377063596pln" style="color:rgb(48,51,54)">object</span><span class="m_-764863579377063596pun" style="color:rgb(48,51,54)">):</span><span class="m_-764863579377063596pln" style="color:rgb(48,51,54)">
</span><span class="m_-764863579377063596str" style="color:rgb(125,39,39)">"""A generator that iterates through a file"""</span><span class="m_-764863579377063596pln" style="color:rgb(48,51,54)">
</span><span class="m_-764863579377063596kwd" style="color:rgb(16,16,148)">def</span><span class="m_-764863579377063596pln" style="color:rgb(48,51,54)"> __init__</span><span class="m_-764863579377063596pun" style="color:rgb(48,51,54)">(</span><span class="m_-764863579377063596pln" style="color:rgb(48,51,54)">self</span><span class="m_-764863579377063596pun" style="color:rgb(48,51,54)">,</span><span class="m_-764863579377063596pln" style="color:rgb(48,51,54)"> path</span><span class="m_-764863579377063596pun" style="color:rgb(48,51,54)">):</span><span class="m_-764863579377063596pln" style="color:rgb(48,51,54)">
self</span><span class="m_-764863579377063596pun" style="color:rgb(48,51,54)">.</span><span class="m_-764863579377063596pln" style="color:rgb(48,51,54)">path </span><span class="m_-764863579377063596pun" style="color:rgb(48,51,54)">=</span><span class="m_-764863579377063596pln" style="color:rgb(48,51,54)"> path</span></code></pre></div><div><pre class="m_-764863579377063596lang-py m_-764863579377063596prettyprint m_-764863579377063596prettyprinted" 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"><span class="m_-764863579377063596pln" style="color:rgb(48,51,54)"> </span><span class="m_-764863579377063596kwd" style="color:rgb(16,16,148)">def</span><span class="m_-764863579377063596pln" style="color:rgb(48,51,54)"> __iter__</span><span class="m_-764863579377063596pun" style="color:rgb(48,51,54)">(</span><span class="m_-764863579377063596pln" style="color:rgb(48,51,54)">self</span><span class="m_-764863579377063596pun" style="color:rgb(48,51,54)">):</span><span class="m_-764863579377063596pln" style="color:rgb(48,51,54)">
</span><span class="m_-764863579377063596kwd" style="color:rgb(16,16,148)">with</span><span class="m_-764863579377063596pln" style="color:rgb(48,51,54)"> open</span><span class="m_-764863579377063596pun" style="color:rgb(48,51,54)">(</span><span class="m_-764863579377063596pln" style="color:rgb(48,51,54)">self</span><span class="m_-764863579377063596pun" style="color:rgb(48,51,54)">.</span><span class="m_-764863579377063596pln" style="color:rgb(48,51,54)">path</span><span class="m_-764863579377063596pun" style="color:rgb(48,51,54)">)</span><span class="m_-764863579377063596pln" style="color:rgb(48,51,54)"> </span><span class="m_-764863579377063596kwd" style="color:rgb(16,16,148)">as</span><span class="m_-764863579377063596pln" style="color:rgb(48,51,54)"> f</span><span class="m_-764863579377063596pun" style="color:rgb(48,51,54)">:</span><span class="m_-764863579377063596pln" style="color:rgb(48,51,54)">
</span><span class="m_-764863579377063596pln"><font color="#101094">yield from f</font></span></code></pre></div><div><br></div><div>Best,</div><div><br></div><div>Neil<div><div class="h5"><br></div></div></div></div></blockquote></div><br></div><div class="gmail_extra">I think the difference is that this new approach guarantees cleanup the exact moment the loop ends, no matter how it ends. <br><br>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></div></div>