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
綁定到它並相應地增加其引用計數?