0
>>> a = ()
>>> b = ()
>>> a is b
True
>>> a = (1, 2)
>>> b = (1, 2)
>>> a is b
False

I was expecting that in the second case also 'a' would be the same as 'b'. Since tuples are immutable, I thought they would have the same id. But as 'a is b' returns false, it means they are different objects with the same value. But in the first case(where both of them are empty tuples), they share the same id.

  • If every tuple created had to be interned (so that equal tuples always have the same id) that would be really bad for performance. Beware what you ask for ... – Stephen C Feb 05 '23 at 07:35
  • "Since tuples are immutable, I thought they would have the same id" That is an optimization that the interpret is *free to implement as an optimization*, however, it is not anything guaranteed by the language. Whether some literals that evaluate to the same value get optimized like this is an implementation detail, and those details involve precisely what gets compiled together as a code block where this would even happen. – juanpa.arrivillaga Feb 05 '23 at 08:31

0 Answers0