I get some rather unexpected behavior on an expression that works with ==
but not with is
:
>>> (True == False) is False True >>> True == (False is False) True >>> True == False is False False >>> id(True) 8978640 >>> id(False) 8978192 >>> id(True == False) 8978192 >>> id(False is False) 8978640
In Python, the two Boolean values are True and False (the capitalization must be exactly as shown), and the Python type is bool. In the first statement, the two operands evaluate to equal values, so the expression evaluates to True; in the second statement, 5 is not equal to 6, so we get False.
If the left side of the expression is "falsey", the expression will return the left side. If the left side of the expression is "truthy", the expression will return the right side. That's it. So in false && false , the left side is "falsey", so the expression returns the left side, false .
FALSE evaluates to TRUE, so xor(FALSE, TRUE) evaluates to TRUE. On the other hand if the first argument was changed to 5 == 5 and the second argument was unchanged then both arguments would have been TRUE, so xor(TRUE, TRUE) would have evaluated to FALSE.
Because in fact that's a chained comparison, so
True == False is False
is equivalent to
(True == False) and (False is False)
This can be surprising in this case, but lets you write 1 <= x < 4
unlike in other languages like C.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With