【发布时间】:2016-04-24 20:32:56
【问题描述】:
__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__" 相同 - 显然不是,所以您假设 "instances 从类继承__weakref__" 不正确。A.__weakref__ is A().__weakref__计算结果为False。
标签: python weak-references python-internals