Why does `None is None is None` return True? [duplicate]

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)

Leave a Comment

404 Not Found

Not Found

The requested URL was not found on this server.

Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.