我正在阅读传入包的源代码asyncio
。请注意,在方法的末尾,有一个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