为什么“True == 不是 False”语法错误?

发布于 2024-11-09 16:17:28 字数 674 浏览 0 评论 0原文

在 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 == 在任何 < 的地方都是有效的语法/code> 是一个具有有效语法的表达式。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

云归处 2024-11-16 16:17:28

它与 Python 中的运算符优先级 有关(解释器认为你'正在比较 True 与 not,因为 == 的优先级高于 not)。您需要一些括号来阐明运算顺序:

True == (not False)

一般来说,如果没有括号,则不能在比较的右侧使用 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 than not). You need some parentheses to clarify the order of operations:

True == (not False)

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 a not on the right side of a comparison.

绳情 2024-11-16 16:17:28

声称 True == not False 构成语法错误的原因与运算符优先级有关的答案是错误的。如果是这种情况,表达式 2 ** - 1 也会产生语法错误,当然事实并非如此。优先级永远不会导致使用运算符来代替操作数。

True == not False 是语法错误的真正原因是不存在会产生 比较,因为

比较 ::= or_expr (comp_operator or_expr)*

- 即在 comp_operator == 之后 or_expr 必须跟随,其中包括 xor_expr、and_expr、shift_expr、a_expr、m_expr,一个 u_expr,一个 power…,但没有 not_test

相比之下,优先级相似的​​构造2 ** - 1符合幂律

power ::= (await_expr | Primary) ["**" u_expr]

让 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 expression 2 ** - 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

comparison ::= or_expr (comp_operator or_expr)*

- 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 rule

power ::= (await_expr | primary) ["**" u_expr]

has u_expr following the power operator **, thus allowing - x on the right hand side.

情释 2024-11-16 16:17:28

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.

韬韬不绝 2024-11-16 16:17:28

我认为你正在寻找的是“而不是”。这将为您提供您想要的结果。如果您比较的布尔值是复合布尔表达式,这里是一个示例网站 复合布尔表达式

>>> True and True
True
>>> True and not True
False
>>> True and not False
True
>>> False and not True
False
>>> False and not False
False
>>> False and False
False

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.

>>> True and True
True
>>> True and not True
False
>>> True and not False
True
>>> False and not True
False
>>> False and not False
False
>>> False and False
False
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文