为什么“True == 不是 False”语法错误?
在 Python 中可以将布尔值与 ==
进行比较。但是当我应用布尔 not
运算符时,结果是语法错误:
Python 2.7 (r27:82500, Sep 16 2010, 18:02:00)
[GCC 4.5.1 20100907 (Red Hat 4.5.1-3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> True == True
True
>>> False == False
True
>>> True is not False
True
>>> True == not False
File "<stdin>", line 1
True == not False
^
SyntaxError: invalid syntax
>>>
为什么这是语法错误?我希望 not False
是一个返回布尔值的表达式,并且 True ==
在任何
Comparing boolean values with ==
works in Python. But when I apply the boolean not
operator, the result is a syntax error:
Python 2.7 (r27:82500, Sep 16 2010, 18:02:00)
[GCC 4.5.1 20100907 (Red Hat 4.5.1-3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> True == True
True
>>> False == False
True
>>> True is not False
True
>>> True == not False
File "<stdin>", line 1
True == not False
^
SyntaxError: invalid syntax
>>>
Why is this a syntax error? I would expect not False
to be an expression that returns a boolean value, and True == <x>
to be valid syntax wherever <x>
is an expression with valid syntax.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
它与 Python 中的运算符优先级 有关(解释器认为你'正在比较 True 与 not,因为
==
的优先级高于not
)。您需要一些括号来阐明运算顺序:一般来说,如果没有括号,则不能在比较的右侧使用
not
。但是,我想不出在比较的右侧需要使用not
的情况。It has to do with operator precedence in Python (the interpreter thinks you're comparing True to not, since
==
has a higher precedence thannot
). You need some parentheses to clarify the order of operations:In general, you can't use
not
on the right side of a comparison without parentheses. However, I can't think of a situation in which you'd ever need to use anot
on the right side of a comparison.声称
True == not False
构成语法错误的原因与运算符优先级有关的答案是错误的。如果是这种情况,表达式2 ** - 1
也会产生语法错误,当然事实并非如此。优先级永远不会导致使用运算符来代替操作数。True == not False
是语法错误的真正原因是不存在会产生 比较,因为- 即在 comp_operator
==
之后 or_expr 必须跟随,其中包括 xor_expr、and_expr、shift_expr、a_expr、m_expr,一个 u_expr,一个 power…,但没有 not_test。相比之下,优先级相似的构造
2 ** - 1
符合幂律让 u_expr 跟随幂运算符
**
,从而允许- x
位于右侧。Answers claiming that the reason for
True == not False
constituting a syntax error had to do with operator precedence are mistaken. If that were the case, the expression2 ** - 1
would yield a syntax error as well, which of course it doesn't. Precedence never causes an operator to be drawn in in place of an operand.The true reason for
True == not False
being a syntax error is that there exists no syntax rule that would produce a comparison therefrom, since- i. e. after the comp_operator
==
an or_expr must follow, which includes an xor_expr, an and_expr, a shift_expr, an a_expr, an m_expr, an u_expr, a power…, but no not_test.By comparison, the precedence-wise similar construct
2 ** - 1
in accordance with the power rulehas u_expr following the power operator
**
, thus allowing- x
on the right hand side.Python 有一个运算符优先级(这就像数学中的 Bodmas。某些运算符在其他运算符之前被考虑。例如:乘法运算符在加法之前被考虑)。在 python 中,运算符优先级中“==”位于“not”之前。因此,在你的代码行中,Python 分析的第一件事是“False == not”。由于这是不正确的语法,因此会引发错误。
Python has an operator precedence (This is like Bodmas in maths. Certain operators are considered before others. Eg: multiplication operator is considered before addition). In python '==' comes before 'not' in the operator precedence. Therefore, in your line of code, the first thing that Python analyses is 'False == not'. Because this is incorrect syntax, an error is raised.
我认为你正在寻找的是“而不是”。这将为您提供您想要的结果。如果您比较的布尔值是复合布尔表达式,这里是一个示例网站 复合布尔表达式。
I think what you are looking for is "and not". This gives you the results you are looking towards. If your comparing booleans what you have is a compound boolean expression, here is an example website Compound Boolean Expression.