python 中 not() 的顺序有什么问题?

What's wrong with order for not() in python?

在 python 中使用 not() 有什么问题?我试过这个

    In [1]: not(1) + 1
    Out[1]: False

而且效果很好。但是重新调整后,

    In [2]: 1 + not(1)
    Out[2]: SyntaxError: invalid syntax

报错。顺序如何重要?

not 是一个 unary operator,不是函数,因此请不要在其上使用 (..) 调用符号。解析表达式时括号被忽略,not(1) + 1not 1 + 1.

相同

由于优先规则 Python 尝试将第二个表达式解析为:

1 (+ not) 1

这是无效的语法。如果你真的必须在 + 之后使用 not,请使用括号:

1 + (not 1)

出于同样的原因,not 1 + 1 首先计算 1 + 1,然后将 not 应用于结果。