Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sys.getrefcount() returns value much higher than 2

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

like image 769
meallhour Avatar asked Dec 10 '25 07:12

meallhour


1 Answers

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.

like image 99
Jasmijn Avatar answered Dec 11 '25 20:12

Jasmijn