The power operator binds more tightly than unary operators on its
left; it binds less tightly than unary operators on its right.
Thus, in an unparenthesized sequence of power and unary operators, the
operators are evaluated from right to left (this does not constrain
the evaluation order for the operands): -1**2 results in -1.
因为它的工作是1而不是1作为一个整体。这将产生预期的结果。
它与运算符优先级有关。
试试看
首先对
**
求值,然后对-
求值。因此,你得到了-1
。对于
pow
函数,首先评估-1
。参见https://docs.python.org/2/reference/expressions.html#the-power-operator上的参考
从Python文档中: https://docs.python.org/3/reference/expressions.html#the-power-operator
相关问题 更多 >
编程相关推荐