import sys
a = 10
b = a
print sys.getrefcount(a)
b=1
print sys.getrefcount(b)
output:
22
614
Is there any problem with my python interpreter? Why is this giving huge values like 614?
Python version
/usr/lib/python2.7/
import sys
a = 10
b = a
print sys.getrefcount(a)
b=1
print sys.getrefcount(b)
output:
22
614
Is there any problem with my python interpreter? Why is this giving huge values like 614?
Python version
/usr/lib/python2.7/
I also had this question. I read a blog post however that advises when you import a library (like sys), you're giving the environment access to all of the additional variables that this library also references, which is why you might see a prevalence of common integers like 128, 256, etc, when trying to count their references after importing any library.
For example, the integer 1 is referenced 129 times in the sys library (plus one for referencing that integer in code here):
import sys
print(sys.getrefcount(1))
# 130
However, I was puzzled by the example code that I was using to test the getrefcount() functionality:
import sys
print(sys.getrefcount("a highly unique string that certainly doesn't exist anywhere in sys, much less 3 times"))
# 3
The only way I can rationalize this code is by inferring from Python's inheritance scheme. This string of course inherits from the array class, which further inherits from the object class. So even though the string is singular and unique, there is a reference count of 3.
This is proven with a test on the initial code example:
import sys
a = 1
print(sys.getrefcount(1))
# 132
We expected this code to produce at least 130 references, but there are now two more, because 1 is a number, which inherits from object, and so adding the extra a = 1 adds two more references to the number 1.