Why in Python does "0, 0 == (0, 0)" equal "(0, False)"?
In Python (I checked only with Python 3.6 but I believe it should hold for many of the previous versions as well):
(0, 0) == 0, 0 # results in a two element tuple: (False, 0) 0, 0 == (0, 0) # results in a two element tuple: (0, False) (0, 0) == (0, 0) # results in a boolean True
a = 0, 0 b = (0, 0) a == b # results in a boolean True
Why does the result differ between the two approaches? Does the equality operator handle tuples differently?
The first two expressions both parse as tuples:
(0, 0) == 0(which is
False), followed by
0, followed by
0 == (0, 0)(which is still
Falsethat way around).
The expressions are split that way because of the relative precedence of the comma separator compared to the equality operator: Python sees a tuple containing two expressions, one of which happens to be an equality test, instead of an equality test between two tuples.
But in your third example,
a = 0, 0 cannot be a tuple. A tuple is a collection of values, and unlike an equality test, assignment has no value in Python. An assignment is not an expression, but a statement; it does not have a value that can be included into a tuple or any other surrounding expression. If you tried something like
(a = 0), 0 in order to force interpretation as a tuple, you would get a syntax error. That leaves the assignment of a tuple to a variable – which could be made more explicit by writing it
a = (0, 0) – as the only valid interpretation of
a = 0, 0.