0

I understand that the is operator is used to check whether one object points to the same memory address as another. Consider this following example:

>>> x = 5
>>> y = 10
>>> x is x
True
>>> x is y
False
>>> id(x)
499843696
>>> id(y)
499843856

Fair enough. But then I tried this comparison:

>>> x is x is x
True

I'm not sure why that evaluates to True. It's obviously dissimilar to this:

>>> (x is x) is x
False

Because (x is x) returns True and (True) is x when x is 5 will return False. So what is happening in the x is x is x line? For comparison's sake:

>>> x is x is y
False

Is this syntax basically checking whether each object compared points to the memory address? I'm curious as to how that syntax is evaluated across the line.

blacksite
  • 12,086
  • 10
  • 64
  • 109
  • 4
    Chained comparisons execute as `(x is x) and (x is x)` (but the middle `x` is only evaluated once). – Martijn Pieters May 04 '17 at 13:54
  • @Cleared on my Python 3.6 x is x is y gives False. As for the question - I never really thought of this - just knew that its a way how it works :) if you try: x = a then x is x is a is x is a etc as well gives True so I assume Python just compares whole statement not the result (True/False) against following values, and it seems logical to me :) Anyway - nested is usage is rare - at least in my practice – Drako May 04 '17 at 14:08

0 Answers0