I tried to run sys.getrefcount
for int
(11) as below.
>>> a = 11
>>> b = 11
>>> id(a)
1845083728496
>>> id(b)
1845083728496
>>> id(11)
1845083728496
>>> import sys
>>> sys.getrefcount(a)
17
>>> sys.getrefcount(b)
17
>>> sys.getrefcount(11)
20
Why am i getting the result of 20 references to the number 11
and why it is higher than the reference count returned from a
and b
variables?.
But when i run the same as a standalone script, i am getting the same reference count for a
, b
and 11
, as below.
import sys
a = 11
b = 11
print("a : {}".format(a))
print("b : {}".format(b))
print('-----------------')
print("hex of a: {}".format(hex(id(a))))
print("hex of a: {}".format(hex(id(b))))
print("hex of 11: {}".format(hex(id(11))))
print('-----------------')
print("ref count of a: {}".format(sys.getrefcount(a)))
print("ref count of b: {}".format(sys.getrefcount(b)))
print("ref count of 11: {}".format(sys.getrefcount(11)))
Output:
a : 11
b : 11
-----------------
hex of a: 0x2dfac546a70
hex of a: 0x2dfac546a70
hex of 11: 0x2dfac546a70
-----------------
ref count of a: 28
ref count of b: 28
ref count of 11: 28