3

So i've been trying a few things out in python and happened to come across this:

>>> a = 10
>>> b = 10
>>> a is b
True

Apparently when creating the variable b Python notices that there is already another(different) variable with the value 10 and simply creates a reference to it (simply to save memory, maybe?). Since integers are immutable (at least I think they are) it makes some kind of sense. But then i tried the same thing with a bigger number and got this:

>>> a = 100200103847239642631982367
>>> b = 100200103847239642631982367
>>> a is b
False

Here, for some reason Python seems to create another int object instead of making the variable b a reference to the variable a, which does not make sense to me. Assuming the reason the references created in first example is to save memory, wouldn't it be even more efficient to create a reference in the latter case also since the numbers are way bigger?

Parzival
  • 95
  • 6

1 Answers1

3

Python normally caches integers between -5 and 256 (although this may differ between implementation); when two names point to the same cached integer, they have the same id, and thus point to the same object:

>>> c = 10
>>> d = 10
>>> id(c) == id(d)
True
>>> c is d
True

Once you breach that cache threshold, however, the ids will change:

>>> e = 256
>>> d = 256
>>> id(e) == id(d)
True
>>> d = 257
>>> e = 257
>>> id(d) == id(e)
False
>>> d is e
False
>>> f = -5
>>> g = -5
>>> id(f) == id(g)
True
>>> f = -6
>>> g = -6
>>> id(f) == id(g)
False

You are seeing the same effect.

Keep in mind that is does not compare values, do not use is when you really mean "equals to":

>>> 10 * 1000 is 10000
False
>>> 10 * 1000 == 10000
True
Burhan Khalid
  • 169,990
  • 18
  • 245
  • 284
  • I was reading the docs trying to understand this behaviour, but couldn't find how it caches small data. Thanks for your clarification !! – AlvaroAV Dec 04 '14 at 11:27
  • Thanks for the answer, but why exactly are the limits for caching at -5 and 256? Wouldn't caching make more sense the larger the numbers get? – Parzival Dec 04 '14 at 11:31
  • 1
    @Parzival the point of the caching is for those numbers that appear frequently (e.g. as literals) in code; although caching a larger number saves more memory in theory, they don't appear often enough to be worth it. Caching your `0`s and `-1`s, on the other hand... – jonrsharpe Dec 04 '14 at 11:54