Friday, May 1, 2020

Why does `True == False is False` evaluate to False in Python? (2013)


From the docs:

x < y <= z is equivalent to x < y and y <= z, except that y is evaluated only once (but in both cases z is not evaluated at all when x < y is found to be false).

In your case True == False is False is equivalent to True == False and False is False as the first condition is False so it short-circuits and return False.

>>> dis.dis(lambda : True == False is False) 1 0 LOAD_GLOBAL 0 (True) 3 LOAD_GLOBAL 1 (False) 6 DUP_TOP 7 ROT_THREE 8 COMPARE_OP 2 (==) 11 JUMP_IF_FALSE_OR_POP 21 <---------this step 14 LOAD_GLOBAL 1 (False) 17 COMPARE_OP 8 (is) 20 RETURN_VALUE >> 21 ROT_TWO 22 POP_TOP 23 RETURN_VALUE 


from Hacker News https://ift.tt/3bWPgwc

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.