The first two expressions both parse as tuples:
(0, 0) == 0
(which is False
), followed by 0
0
, followed by 0 == (0, 0)
(which is still False
that 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 second set of statements, 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
.
So even without the parentheses on the assignment to a
, both it and b
get assigned the value (0,0)
, so a == b
is therefore True
.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…