5.4 幂运算符 The power operator

The power operator binds more tightly than unary operators on its left; it binds less tightly than unary operators on its right. The syntax is:

幂运算符比在操作数左边的一元运算符有更高的优先级; 但比右面的一元运算符要低.句法为:

power  ::=  primary ["**" u_expr]
Download entire grammar as text.

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).

因此, 在一串没有括号的由幂运算符和一元运算符组成的序列, 会从左到右面求值(没有强制改变求值顺序).

The power operator has the same semantics as the built-in pow() function, when called with two arguments: it yields its left argument raised to the power of its right argument. The numeric arguments are first converted to a common type. The result type is that of the arguments after coercion.

当以两个参数调用pow() 时, 幂运算符与内建函数 pow()有相同的语义: 生成左边参数的右边参数次方.数值型参数首先转换成通用类型. 结果类型是参数经强制规则转换后的结果; 若结果不能以该类型表达(计算整数的幂结果为负数, 或计算负浮点数的幂为无效的值), 则引发一个TypeError 异常.

With mixed operand types, the coercion rules for binary arithmetic operators apply. For int and long int operands, the result has the same type as the operands (after coercion) unless the second argument is negative; in that case, all arguments are converted to float and a float result is delivered. For example, 10**2 returns 100, but 10**-2 returns 0.01. (This last feature was added in Python 2.2. In Python 2.1 and before, if both arguments were of integer types and the second argument was negative, an exception was raised).

Raising 0.0 to a negative power results in a ZeroDivisionError. Raising a negative number to a fractional power results in a ValueError.