Why does None is None is None return True?
Today, in an interview, the CTO asked me what looks like an easy question,
What does this statement return ? :
None is None is None
I thought Python executed the first operation
None is None and would return
True. After that, it would compare
True is None which would return
False. But, to my surprise, the right answer is
True. I am trying to find answer to this question, but after a couple of days searching I didn't find anything. Can someone explain why this happens?
The bytecode shows that two comparisons are being performed here with the middle being duplicated:
>>> import dis >>> def a(): ... return None is None is None ... >>> dis.dis(a) 2 0 LOAD_CONST 0 (None) 3 LOAD_CONST 0 (None) 6 DUP_TOP 7 ROT_THREE 8 COMPARE_OP 8 (is) 11 JUMP_IF_FALSE_OR_POP 21 14 LOAD_CONST 0 (None) 17 COMPARE_OP 8 (is) 20 RETURN_VALUE >> 21 ROT_TWO 22 POP_TOP 23 RETURN_VALUE
As stated in the docs for comparisons this is because these operators chain together.
a op b op c will be translated to
a op b and b op c (note
b is duplicated in the bytecode as shown above)