Home > Net >  sys.getrefcount() returns value much higher than 2
sys.getrefcount() returns value much higher than 2

Time:11-29

I understand that calling getrefcount() copies reference by value into the function's argument, temporarily bumping up the object's reference count. This is where the second reference comes from.

However, when I run the following code I am getting reference count value of 33

import sys
a1 = 5
print(sys.getrefcount(a1))

I was expecting an output of 2 but it is printing 33

CodePudding user response:

5 is one of the small integers CPython caches. Every value that happens to be 5 thus points to the same instance. Change it to, for example, a1 = 5000 and you'll see it print 2 as expected.

  • Related