今天爱分享给大家带来为什么1 in [1,0] == True执行结果是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
这里python使用了比较运算符链
1 in [1,0] == True
将被转为
(1 in [1, 0]) and ([1, 0] == True)
很显然是false的
同样的
a < b < c
会被转为
(a < b) and (b < c) # b不会被解析两次