1
出于好奇,我想了解引用计数是如何在Python中起作用的。这两个条目:仍然对引用计数感到困惑
- Why are Python Ref Counts to small integers surprisingly high?
- Is there a way to get the current ref count of an object in Python?
是有帮助的,但仍然提出的问题。
使用
sys.getrefcount()
返回不同的值比len(gc.get_referrers())
。例如:>>> a = 3 >>> print sys.getrefcount(a) 38 >>> print len(gc.get_referrers(a)) 23
为什么区别?
据我所知,上
a
引用计数是如此之高,因为已经有在我结合的名称a
给它的时间保持的3
一个整数值的对象。 Python如何跟踪哪个对象持有3
,以便它将名称a
绑定到它并相应地增加其引用计数?