Small integer caching in python
I noticed something interesting about python >>> a=10 >>> b=10 >>> print(a is b) True But also, >>> a=1000 >>> b=1000 >>> print(a is b) False The small integer cache Python (specifically CPython) pre-caches integers in the range -5 to 256 at interpreter startup. These integers are interned, meaning there’s only one shared object for each of them. So when you do: >>> a = 42 >>> b = 42 Both a and b point to the same memory address, because 42 is inside the small-int cache. That’s why a is b is True. ...