<p>因为<code>func</code>是一个绑定方法,因此间接引用它绑定到的对象,这意味着您正在创建一个引用循环。你知道吗</p>
<p>您可以通过执行以下操作来验证这一点:</p>
<pre><code>...
if __name__ == '__main__':
import sys
print(sys.getrefcount(tD()))
</code></pre>
<p>在您的案例2和案例4中应该打印<code>1</code>,在案例3中应该打印<code>2</code>。你知道吗</p>
<p><a href="https://docs.python.org/2/reference/datamodel.html?#object.__del__" rel="nofollow">^{<cd4>}</a>的文档中有一个关于引用循环的注释:</p>
<blockquote>
<p>[...] Some common situations that may prevent the reference count of an object from going to zero include: circular references between objects (e.g., a doubly-linked list or a tree data structure with parent and child pointers); a reference to the object on the stack frame of a function that caught an exception (the traceback stored in sys.exc_traceback keeps the stack frame alive); or a reference to the object on the stack frame that raised an unhandled exception in interactive mode (the traceback stored in sys.last_traceback keeps the stack frame alive)<br/>
[...]<br/>
Circular references which are garbage are detected when the option cycle detector is enabled (it’s on by default), but can only be cleaned up if there are no Python-level <code>__del__()</code> methods involved.</p>
</blockquote>
<p>基本上,这意味着如果您有一个<code>__del__</code>方法,它将阻止清理包含引用循环的对象。你知道吗</p>