为什么“not(True) in [False, True]"返回错误? [英] Why does "not(True) in [False, True]" return False?
本文介绍了为什么“not(True) in [False, True]"返回错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果我这样做:
<预><代码>>>>[假,真]中的假真的返回 True
.仅仅因为 False
在列表中.
但如果我这样做:
<预><代码>>>>not(True) 在 [False, True]错误的返回False
.而 not(True)
等于 False
:
为什么?
解决方案
运算符优先级 2.x, 3.x.not
的优先级低于 in
的优先级.所以等价于:
这就是你想要的:
<预><代码>>>>(非真)在 [假,真]真的<小时>
正如@Ben 指出的那样:建议永远不要写 not(True)
,更喜欢 not True
.前者使它看起来像一个函数调用,而 not
是一个运算符,而不是一个函数.
If I do this:
>>> False in [False, True]
True
That returns True
. Simply because False
is in the list.
But if I do:
>>> not(True) in [False, True]
False
That returns False
. Whereas not(True)
is equal to False
:
>>> not(True)
False
Why?
解决方案
Operator precedence 2.x, 3.x. The precedence of not
is lower than that of in
. So it is equivalent to:
>>> not ((True) in [False, True])
False
This is what you want:
>>> (not True) in [False, True]
True
As @Ben points out: It's recommended to never write not(True)
, prefer not True
. The former makes it look like a function call, while not
is an operator, not a function.
这篇关于为什么“not(True) in [False, True]"返回错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文