Python重用相同的内存位置,因为没有其他对对象的引用,一旦对id(a.f)求值,就会有更多对该对象的引用,因此Python就可以为a.g重用相同的内存位置。如果将方法指定给名称,您将看到不同的行为:# creates a reference to the method f
In [190]: f = a.f
# creates a reference to the method g
In [191]: g = a.g
# cannot reuse the memory location of f as it is still referenced
In [192]: id(f) == id(g)
Out[192]: False
实际上,您只需要存储一个对f的引用就可以看到与上述相同的行为。在
^{pr2}$
您可以看到带有^{}或^{}的引用计数:In [2]: import gc
In [3]: f = a.f
In [4]: len(gc.get_referrers(a.g)),len(gc.get_referrers(f))
Out[4]: (0, 1)
In [5]: sys.getrefcount(a.g),sys.getrefcount(f)
Out[5]: (1, 2)
您看到a.g为1的唯一原因是返回的计数通常比您预期的高一个,因为它包含(临时)引用作为getrefcount()的参数。
这与您自己的示例类似,在对方法求值之后,您仍然会引用f,使用a.g,refcount将为0,因此它将立即被垃圾回收,python可以自由地将内存位置用于任何其他操作。在
值得注意的是,行为不仅限于方法,它只是cpython实现的细节,而不是您应该依赖的东西:In [67]: id([]), id([])
Out[67]: (139746946179848, 139746946179848)
In [73]: id(tuple()),id([]),id([])
Out[73]: (139747414818888, 139746946217544, 139746946217544)
In [74]: id([]),id([]),id([])
Out[74]: (139746946182024, 139746946182024, 139746946182024)
In [75]: id([]),id(tuple()),id([])
Out[75]: (139746946186888, 139747414818888, 139746946186888)
In [76]: id(tuple()),id([]),id(tuple())
Out[76]: (139747414818888, 139746946217736, 139747414818888)