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?