我在尝试删除对象的最后一个引用时遇到了一个奇怪的问题。
代码是:
import sys
import weakref
class Ref:
def __init__(self, name):
self.name = name
self.ref = []
def reference(self, obj):
name = obj.name
self.ref.append(
weakref.ref(obj, lambda wref: print('{!r} is dead.'.format(name))))
a = Ref('a')
b = Ref('b')
c = Ref('c')
d = Ref('d')
a.reference(b)
b.reference(c)
c.reference(d)
d.reference(a)
print('reference count before killed:', sys.getrefcount(d.ref[0]()))
del a
print('reference count after killed:', sys.getrefcount(d.ref[0]()))
输出是这样的:
reference count before killed: 2
'a' is dead.
reference count after killed: 1547
'd' is dead.
'c' is dead.
但有时(这完全是随机的)我只收到'd' is dead.
or 'c' is dead.
(但从来没有'b' is dead.
),或者根本没有收到这些消息。
所以我的第一个问题是:这个奇怪的引用计数是什么1547
?它来自哪里?
第二个是:为什么杀死实例a
会产生这种随机的“杀死其他实例”效果?