The Python "is" statement and tuples

comparison, python, tuples

Solution

`is` tests to see if both sides of the statement share the same memory address. It's basically a shorthand for `id(a) == id(b)`

>>> print id(()), id(())
30085168 30085168
>>> print id((0,)), id((0,))
38560624 38676432
>>>

As `()` happens fairly frequently, it is actually treated as a singleton by the Python Interpreter (just like integers from 0 to 255, empty strings, empty lists, etc.). When comparing `(0, )` to `(0, )` to the interpreter they are actually different variables in memory. If they were mutable, you could modify the first, and the second wouldn't change, hence they are not the same (`a is not b`).

Problem

Why is `() is ()` true, yet `(0,) is (0,)` is false? I thought they would be the same object. However, I'm apparently missing something.

Original source