问题:为什么([1,0]中的1 = = True)评估为False?

在查看该问题的答案时,我发现自己不明白自己的答案。

我真的不明白这是如何解析的。为什么第二个示例返回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 "<pyshell#4>", line 1, in <module>
    1 in ([1,0] == True)
TypeError: argument of type 'bool' is not iterable

谢谢你的帮助。我想我一定会错过一些非常明显的东西。


我认为这与链接的重复项有细微的不同:

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

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

看着0 < 0 == 0您可以想象每个被评估的部分都可以表达为有意义:

>>> (0 < 0) == 0
True
>>> 0 < (0 == 0)
True

因此,链接回答了为什么这样评估False

>>> 0 < 0 == 0
False

但是,对于我的示例1 in ([1,0] == True),将其作为一个表达式没有任何意义,因此,除了存在两种(公认错误的)可能的解释之外,似乎只有一种可能:

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

When I was looking at answers to this question, I found I didn’t understand my own answer.

I don’t really understand how this is being parsed. Why does the second example return 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 "<pyshell#4>", line 1, in <module>
    1 in ([1,0] == True)
TypeError: argument of type 'bool' is not iterable

Thanks for any help. I think I must be missing something really obvious.


I think this is subtly different to the linked duplicate:

Why does the expression 0 < 0 == 0 return False in Python?.

Both questions are to do with human comprehension of the expression. There seemed to be two ways (to my mind) of evaluating the expression. Of course neither were correct, but in my example, the last interpretation is impossible.

Looking at 0 < 0 == 0 you could imagine each half being evaluated and making sense as an expression:

>>> (0 < 0) == 0
True
>>> 0 < (0 == 0)
True

So the link answers why this evaluates False:

>>> 0 < 0 == 0
False

But with my example 1 in ([1,0] == True) doesn’t make sense as an expression, so instead of there being two (admittedly wrong) possible interpretations, only one seems possible:

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

回答 0

Python实际上在这里应用比较运算符链接。表达式被翻译成

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

这显然是False

类似的表达式也会发生这种情况

a < b < c

转换为

(a < b) and (b < c)

(无需b两次评估)。

有关更多详细信息,请参见Python语言文档

Python actually applies comparison operator chaining here. The expression is translated to

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

which is obviously False.

This also happens for expressions like

a < b < c

which translate to

(a < b) and (b < c)

(without evaluating b twice).

See the Python language documentation for further details.


声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。