本文介绍了为什么“not(True) in [False, True]"返回错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果我这样做:
>>>[假,真]中的假真的返回 True.仅仅因为 False 在列表中.
但如果我这样做:
>>>not(True) 在 [False, True]错误的返回False.而 not(True) 等于 False:
>>>不对)错误的为什么?
解决方案运算符优先级 2.x, 3.x.not 的优先级低于 in 的优先级.所以等价于:
>>>not ((True) in [False, True])错误的这就是你想要的:
>>>(非真)在 [假,真]真的正如@Ben 指出的那样:建议永远不要写 not(True),更喜欢 not True.前者使它看起来像一个函数调用,而 not 是一个运算符,而不是一个函数.
If I do this:
>>> False in [False, True] TrueThat returns True. Simply because False is in the list.
But if I do:
>>> not(True) in [False, True] FalseThat returns False. Whereas not(True) is equal to False:
>>> not(True) FalseWhy?
解决方案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]) FalseThis 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]"返回错误?
发布评论