pythondynamic-typing

In dynamic typing the existing variable's ID isn't changing in python


value = 10

print(value, id(value), type(value))

value = "100"

print(value, id(value), type(value))

Output :

10 1383004224 <class 'int'>
       
100 21775456 <class 'str'>

when I rerun the above code the ID of the TYPE str is changing but the ID of the TYPE int is not changing why?


Solution

  • small integer caching Python caches small integers, which are integers between -5 and 256. So all you need is used for your tests numbers more then 256 or less then -5.