为什么 (1 in [1,0] == True) 评估为 False? [英] Why does (1 in [1,0] == True) evaluate to False?

查看:41
本文介绍了为什么 (1 in [1,0] == True) 评估为 False?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

<预><代码>>>>1 in [1,0] # 这是预期的真的>>>1 in [1,0] == True # 这很奇怪错误的>>>(1 in [1,0]) == True # 这就是我想要的真的>>>1 in ([1,0] == True) # 但这不仅仅是一个优先级问题!# 它没有在第二个例子中引发异常.回溯(最近一次调用最后一次):文件<pyshell#4>",第 1 行,在 <module> 中1 in ([1,0] == True)类型错误:bool"类型的参数不可迭代

感谢您的帮助.我想我一定遗漏了一些非常明显的东西.

<小时>

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

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

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

看着 0 <0 == 0 你可以想象每一半都被评估并作为一个表达式有意义:

<预><代码>>>>(0 <0) == 0真的>>>0<(0 == 0)真的

所以链接回答了为什么这会评估 False:

<预><代码>>>>0<0 == 0错误的

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

<预><代码>>>>(1 in [1,0]) == 真

解决方案

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

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

这显然是False.

这也适用于像

这样的表达式

a <<C

翻译成

(a 

(不计算 b 两次).

有关详细信息,请参阅 Python 语言文档.

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

解决方案

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.

这篇关于为什么 (1 in [1,0] == True) 评估为 False?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆