擅长:python、mysql、java
<p>使用关键字<code>global</code>可以解决您的问题:</p>
<pre><code>def exitfun(self):
global obj
del obj
</code></pre>
<p>然而,这可能不是你想做的。为了定义类方法,您需要<a href="https://docs.python.org/2/library/functions.html#classmethod" rel="nofollow">classmethod</a>修饰符。另外,您可以在类级别保存实例,这样就不必通过<code>globals</code>来访问它:</p>
^{pr2}$