1

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?

cgsdfc
  • 538
  • 4
  • 19
  • For strings, you could have a look at [The internals of Python string interning](http://guilload.com/python-string-interning/) – Thierry Lathuille Oct 17 '18 at 12:34
  • Thanks! But that was python2.7. My question was wrong in that immutable stuffs aren't all singleton. Only those cached like small int's or interned strings are. And the `is` operator cannot be used to compare immutable stuffs for their equality reliably. – cgsdfc Oct 18 '18 at 10:29

0 Answers0