开发者

Why is 'True == not False' a SyntaxError?

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
&开发者_运维问答gt;>> 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.


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.


It's just a matter of operator precedence. Try:

>>> True == (not False)
True

Have a look in this table of operator precedences, you'll find that == binds tigher than not, and thus True == not False is parsed as (True == not) False which is clearly an error.


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.


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜