令人困惑的比较输出
Confusing comparison output
我对以下内容感到困惑:
>>> 1,2 == 1,2
(1, False, 2)
==
运算符应该 return 只是一个布尔值(或者至少我是这么认为的)。
我本来希望 (True, True)
假设该行会像 a,b = 1,2
一样处理,但执行比较而不是赋值。或者,有一个错误。但绝对不是(1, False, 2)
.
谁能解释一下这是怎么回事?
这个:
1,2 == 1,2
被评估为一个三元素元组,分别包含 1
、2 == 1
和 2
。您需要在此处使用几个括号:
(1, 2) == (1, 2)
这在Language Reference中有说明:
Except when part of a list or set display, an expression list
containing at least one comma yields a tuple. The length of the tuple
is the number of expressions in the list. The expressions are
evaluated from left to right.
@alec_djinn
比较运算符工作如果两个操作数的值相等,则条件成立。
您正在尝试比较错误的数据类型 1,2 无效。
试试 '1,2' == '1,2' 会给你正确的结果。
1,2 不是单个参数。
我对以下内容感到困惑:
>>> 1,2 == 1,2
(1, False, 2)
==
运算符应该 return 只是一个布尔值(或者至少我是这么认为的)。
我本来希望 (True, True)
假设该行会像 a,b = 1,2
一样处理,但执行比较而不是赋值。或者,有一个错误。但绝对不是(1, False, 2)
.
谁能解释一下这是怎么回事?
这个:
1,2 == 1,2
被评估为一个三元素元组,分别包含 1
、2 == 1
和 2
。您需要在此处使用几个括号:
(1, 2) == (1, 2)
这在Language Reference中有说明:
Except when part of a list or set display, an expression list containing at least one comma yields a tuple. The length of the tuple is the number of expressions in the list. The expressions are evaluated from left to right.
@alec_djinn
比较运算符工作如果两个操作数的值相等,则条件成立。
您正在尝试比较错误的数据类型 1,2 无效。
试试 '1,2' == '1,2' 会给你正确的结果。
1,2 不是单个参数。