I'm still confused by this #python oddity, originally posted by @driscollis:
x = True
y = False
print(x is (not y))
print(not x is y)
print(not (x is y))
print((not x) is y)
print(x is not y)
print(x == (not y))
print(not x == y)
print(not (x == y))
print((not x) == y)
print(x == not y)
Why is the last line a syntax error when the rest are fine?
@spencer @driscollis ... but Python is perfectly happy to evaluate 5 + - 4, because unary - has higher precedence than +, so this is parsed as 5 + (- 4).
This is the path to the solution, though. == has higher priority than not, so x == not y just doesn't have any reasonable parse tree. (In Java, in contrast, ! has higher priority than ==. I don't know why Python chose to do it differently.)
So how does x is not y work? Because, surprisingly, "is not" is one operator!
@peterdrake
Thanks for clarifying. I didn’t even recognize the „is not“ operator beneath all other comparisons.
@driscollis