-1

We know that the "is" operator compares the id of two objects. But why it happens:
1.

x = [1, 2, 3]
y = [1, 2, 3]
print(x is y)

x = (1, 2, 3)
y = (1, 2, 3)
print(x is y)

Run it as python test.py, the output is:

False
True

Run it in Python interpreter:

>>> x = (1, 2, 3)
>>> y = (1, 2, 3)
>>> x is y
False
x = (1, [2], 3)
y = (1, [2], 3)
print(x is y)

Run it as python test.py, the output is:

False

How Python compare id of objects of different types ?

Jared Zhu
  • 37
  • 2
  • Python appears to be [interning tuples](https://localcoder.org/in-python-when-are-two-objects-the-same); perhaps the interpreter doesn't? In your third example, `x` and `y` can't be the same because the list `[2]` has different IDs (whereas Python does intern integers, meaning they will have the same ID). – LMD May 16 '22 at 09:37
  • Interning of integers only applies to -5 to 256. – Alexander Jordan May 16 '22 at 09:40

0 Answers0