Why is sys.getrefcount giving huge values?
This is due to the fact that internally CPython stores an already created integer object with a value of 1 and internal variables point to it. It makes sense to have only one such object since it is immutable.
The same goes for string literals, as they are immutable compilers, typically holding one unique string in memory and pointing to it in variables.
The more unique a literal is, the less likely it is to be created internally.
>>> sys.getrefcount(1337)
3
>>> sys.getrefcount('p')
14
>>> sys.getrefcount('StackOverflow')
3
As you can see in the internals, some small whole objects are created and cached for a little optimization. https://github.com/python/cpython/blob/2.7/Objects/intobject.c#L74
source to share
You can see the effects of Python optimization (see @ Nether's answer) if you change the values ββto non-trivial. For example, if your code is changed to:
from __future__ import print_function import sys a = 1012345 b = a print(sys.getrefcount(a)) b=12345678 print(sys.getrefcount(b))
Output:
5 4
source to share