python中1∧0,python - 为什么([1,0] == True中的1)计算为False?

ゞ 浴缸里的玫瑰 2023-01-17 04:47 194阅读 0赞

这个问题在这里已有答案:

为什么表达式0< 0 == 0在Python中返回False? 9个答案

当我看到这个问题的答案时,我发现我不明白自己的答案。

我真的不明白这是如何被解析的。 为什么第二个示例返回False?

1 in [1,0] # This is expected

True

1 in [1,0] == True # This is strange

False

(1 in [1,0]) == True # This is what I wanted it to be

True

1 in ([1,0] == True) # But it’s not just a precedence issue!

# It did not raise an exception on the second example.

Traceback (most recent call last):

File “”, line 1, in

1 in ([1,0] == True)

TypeError: argument of type ‘bool’ is not iterable

谢谢你的帮助。 我想我必须遗漏一些非常明显的东西。

我认为这与链接的副本略有不同:

为什么表达式0< 0 == 0在Python中返回False?

这两个问题都与人类对表达的理解有关。 似乎有两种方式(在我看来)评估表达式。 当然两者都不正确,但在我的例子中,最后的解释是不可能的。

看看1 in ([1,0] == True)你可以想象每一半被评估并作为一个表达有意义:

(0 < 0) == 0

True

0 < (0 == 0)

True

所以链接回答了为什么这个评估1 in ([1,0] == True):

0 < 0 == 0

False

但是我的例子1 in ([1,0] == True)作为一个表达式没有意义,所以不是有两个(当然是错误的)可能的解释,只有一个似乎是可能的:

(1 in [1,0]) == True

发表评论

表情:
评论列表 (有 0 条评论,194人围观)

还没有评论,来说两句吧...

相关阅读