Why in Python does “0, 0 == (0, 0)” equal “(0, False)”?

后端 未结 7 989
粉色の甜心
粉色の甜心 2020-12-04 09:20

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 elemen         


        
7条回答
  •  没有蜡笔的小新
    2020-12-04 10:01

    The first two expressions both parse as tuples:

    1. (0, 0) == 0 (which is False), followed by 0
    2. 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.

提交回复
热议问题