Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading