__weakref__
与弱引用相关。我完全理解弱引用的概念以及何时需要使用它们。唯一让我困惑的是以下描述:一个实例本身没有属性
__weakref__
,不同于类,因此实例从类中继承__weakref__
,这意味着A.__weakref__
应该与A().__weakref__
相同:>>> class A: pass
...
>>> A().__dict__ # Each instance starts out as an empty namespace
{}
>>> A.__weakref__ is None;
False
>>> A().__weakref__ is None #But this is True!
True
为什么`A.__weakref__`不是`None`,而`instance.__weakref__`是`None`,即使实例从类中继承了`__weakref__`?
A.__weakref__
和A().__weakref__
是不同的,因此您假设“实例从类继承__weakref__
”是不正确的。A.__weakref__ is A().__weakref__
的结果为False
。 - jonrsharpe