为什么Python说pow只有两个参数

时间:2021-10-06 22:44:11

Why is python telling me "TypeError: pow expected 2 arguments, got 3" despite it working in IDLE (sometimes it tells me that in IDLE as well)? im simply doing pow(a,b,c). my program is very short and i do not change the definition of pow at any time since i need to use it for some exponentiation.

为什么python告诉我“TypeError: pow期望2个参数,得到3”,尽管它在空闲时工作(有时它也告诉我在空闲时)?我只是做战俘(a,b,c)。我的程序很短,而且我在任何时候都不会改变pow的定义,因为我需要用它来做一些指数运算。

NOTE: This is the pow from __builtin__, not Math

注意:这是来自__builtin__的pow,不是数学

3 个解决方案

#1


14  

Built-in pow takes two or three arguments. If you do from math import * then it is replaced by math's pow, which takes only two arguments. My recommendation is to do import math, or explicitly list functions you use in import list. Similar issue happens with open vs. os.open.

内置的pow需要两到三个参数。如果你从math import *中做,那么它就会被math的pow取代,它只需要两个参数。我的建议是导入数学,或者显式地列出在导入列表中使用的函数。类似的问题也发生在open和os.open之间。

#2


1  

If you are using math functions a lot and the three parameter version of pow infrequently a way around this in python 2.7 is to import __builtin__ and call __builtin__.pow for the 3 paramete

如果您经常使用数学函数,而pow的三个参数版本很少使用,那么在python 2.7中,一个方法是导入__builtin__并调用__builtin__。3个参数的pow

#3


0  

http://docs.python.org/release/2.6.5/library/functions.html

http://docs.python.org/release/2.6.5/library/functions.html

pow(x, y[, z]) Return x to the power y; if z is present, return x to the power y, modulo z (computed more efficiently than pow(x, y) % z). The two-argument form pow(x, y) is equivalent to using the power operator: x**y.

pow(x, y[z])返回x的y次方;如果有z,返回x的y次方,模量z(比pow(x, y) % z计算效率更高)。

The arguments must have numeric types. 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, 102 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.) If the second argument is negative, the third argument must be omitted. If z is present, x and y must be of integer types, and y must be non-negative. (This restriction was added in Python 2.2. In Python 2.1 and before, floating 3-argument pow() returned platform-dependent results depending on floating-point rounding accidents.)

参数必须具有数值类型。对于混合操作数类型,应用二进制算术运算符的强制规则。对于int和long int操作数,结果具有与操作数相同的类型(在强制之后),除非第二个参数是负数;在这种情况下,所有的参数都转换为浮点数,并传递一个浮点数。例如,102返回100,但10-2返回0.01。(最后一个特性是在Python 2.2中添加的。在Python 2.1和之前,如果两个参数都是整数类型,第二个参数为负数,则会引发异常。如果第二个参数是负数,则必须省略第三个参数。如果z存在,x和y必须是整数类型,y必须是非负的。(在Python 2.2中添加了这个限制。在Python 2.1和之前,浮点3参数pow()根据浮点舍入事件返回与平台相关的结果。

Perhaps you're violating the bold portion?

也许你违反了大胆的部分?

#1


14  

Built-in pow takes two or three arguments. If you do from math import * then it is replaced by math's pow, which takes only two arguments. My recommendation is to do import math, or explicitly list functions you use in import list. Similar issue happens with open vs. os.open.

内置的pow需要两到三个参数。如果你从math import *中做,那么它就会被math的pow取代,它只需要两个参数。我的建议是导入数学,或者显式地列出在导入列表中使用的函数。类似的问题也发生在open和os.open之间。

#2


1  

If you are using math functions a lot and the three parameter version of pow infrequently a way around this in python 2.7 is to import __builtin__ and call __builtin__.pow for the 3 paramete

如果您经常使用数学函数,而pow的三个参数版本很少使用,那么在python 2.7中,一个方法是导入__builtin__并调用__builtin__。3个参数的pow

#3


0  

http://docs.python.org/release/2.6.5/library/functions.html

http://docs.python.org/release/2.6.5/library/functions.html

pow(x, y[, z]) Return x to the power y; if z is present, return x to the power y, modulo z (computed more efficiently than pow(x, y) % z). The two-argument form pow(x, y) is equivalent to using the power operator: x**y.

pow(x, y[z])返回x的y次方;如果有z,返回x的y次方,模量z(比pow(x, y) % z计算效率更高)。

The arguments must have numeric types. 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, 102 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.) If the second argument is negative, the third argument must be omitted. If z is present, x and y must be of integer types, and y must be non-negative. (This restriction was added in Python 2.2. In Python 2.1 and before, floating 3-argument pow() returned platform-dependent results depending on floating-point rounding accidents.)

参数必须具有数值类型。对于混合操作数类型,应用二进制算术运算符的强制规则。对于int和long int操作数,结果具有与操作数相同的类型(在强制之后),除非第二个参数是负数;在这种情况下,所有的参数都转换为浮点数,并传递一个浮点数。例如,102返回100,但10-2返回0.01。(最后一个特性是在Python 2.2中添加的。在Python 2.1和之前,如果两个参数都是整数类型,第二个参数为负数,则会引发异常。如果第二个参数是负数,则必须省略第三个参数。如果z存在,x和y必须是整数类型,y必须是非负的。(在Python 2.2中添加了这个限制。在Python 2.1和之前,浮点3参数pow()根据浮点舍入事件返回与平台相关的结果。

Perhaps you're violating the bold portion?

也许你违反了大胆的部分?