为什么“not(True) in [False, True]"返回错误?

编程入门 行业动态 更新时间:2024-10-25 18:28:22
本文介绍了为什么“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] 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]"返回错误?

本文发布于:2023-11-24 21:20:31,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1626876.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:错误   True   False   quot

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!