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

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

为什么表达式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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值