【发布时间】:2014-03-07 08:24:21
【问题描述】:
我正在阅读the source code of the incoming asyncio package。请注意,在方法的末尾,有一个self = None 语句。它有什么作用?
def _run(self):
try:
self._callback(*self._args)
except Exception as exc:
msg = 'Exception in callback {}{!r}'.format(self._callback,
self._args)
self._loop.call_exception_handler({
'message': msg,
'exception': exc,
'handle': self,
})
self = None # Needed to break cycles when an exception occurs.
我认为它会删除实例,但以下测试不建议这样做:
class K:
def haha(self):
self = None
a = K()
a.haha()
print(a) # a is still an instance
【问题讨论】:
-
也许问题应该是“为什么将
self设置为None会中断循环?什么循环?”
标签: python garbage-collection python-internals python-asyncio