Immutable does not imply unique identity, like the standard numeric types Fraction
and Decimal
.
>>> from fractions import Fraction
>>> Fraction(1, 3) is Fraction(1, 3)
>>> False
However, for all builtin immutable types like tuple
, str
and int
, their instances have unique identities.
>>> 1 is 1
>>> True
>>> (2-1, 0) is (int('1'), 0)
>>> True
To enforce this it seems to me that python uses some dictionaries to keep track of all the creation of these objects so that we cannot create two objects with the same value. It this hypothesis true? Can somebody kindly explains the mechanisms behind the scene?