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
This is because CPython internally keeps an already created integer object with value 1 and internal variables point to it. It makes sense to have only one such object since it is immutable.
The same thing applies to string literals, since they're immutable compilers generally keep a single unique string in memory and make variables point to it.
The more unique the literal, the less chance there is for it to be created internally.
>>> sys.getrefcount(1337)
3
>>> sys.getrefcount('p')
14
>>> sys.getrefcount('StackOverflow')
3
As you can see in the internals here, some small integer objects are created and cached for some small optimization. https://github.com/python/cpython/blob/2.7/Objects/intobject.c#L74