Closed mobluse closed 2 years ago
Thanks much for the very clear test cases! Super helpful in diagnosing the problem. Python is fairly unique in these 'chained' comparison operations; the snek code for doing this was added recently and turned out to have a selection of issues which your tests uncovered.
I only recently learned that 1 == (2 != 0)
is very different from 1 == 2 != 0
. The first case compares 1 against the value of the (2 != 0)
operation. 2 != 0
is True, which compares equal to 1:
>>> 2 != 0
True
>>> 1 == True
True
>>> 1 == (2 != 0)
True
The second case is what I mean by 'chained comparison' operators. In this case, there are effectively two comparisons linked by a conjunction (and
) operator, and Python says that the second comparison is only done if the first is True (just like other and
operations). So, you can kinda rewrite the second expression as 1 == 2 and 2 != 0
:
>>> 1 == 2
False
>>> 2 != 0
True
>>> 1 == 2 and 2 != 0
False
>>> 1 == 2 != 0
False
The trick with these chained comparisons is that the second operand must not be evaluated twice, so you have to be a bit careful in the implementation.
There is an error if you do
print(1 == 2 != 0)
. This error doesn't occur in other Pythons.