Python Forum

Full Version: Shared reference of variables...
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
What I understand is Python uses shared reference for variables.

I verified by assigning two variables with value of 10 (int). They both had the same memory address.

>>> a = 10
>>> b = 10
>>> hex(id(a))
'0x7fe4741d69c0'
>>> hex(id(b))
'0x7fe4741d69c0'
But when I assigned a value of 500, they have difference memory address.
>>> c = 500
>>> d = 500
>>> hex(id(c))
'0x7fe4669f6a30'
>>> hex(id(d))
'0x7fe4669f6b30'
How come? What changed in value 500?

Thanks.
Python caches small integers,which are integers between -5 and 256.
These numbers are used so frequently that it's better for performance to already have these objects available.