为什么1 in [1,0] == True执行结果是False

今天爱分享给大家带来为什么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不会被解析两次


人已赞赏
Python

python如何在循环中获取下标【附代码】

2020-12-22 16:07:18

Python

python使用 'if x is not None' 还是'if not x is None'

2020-12-22 17:44:43

'); })();