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

2024-09-20 22:54:23 发布

您现在位置:Python中文网/ 问答频道 /正文

为什么python告诉我“TypeError:pow需要2个参数,得到3”,尽管它在IDLE中工作(有时它也告诉我在IDLE中也是这样)?我只是在做pow(a,b,c)。我的程序很短,我不会在任何时候更改pow的定义,因为我需要使用它进行一些指数运算。

注:这是__builtin__中的pow,而不是Math


Tags: 程序参数定义math指数idlebuiltintypeerror
3条回答

内置的pow接受两个或三个参数。如果您执行from math import *,那么它将被math的pow替换,math只接受两个参数。我的建议是执行import math,或者显式地列出在导入列表中使用的函数。类似的问题发生在openos.open之间。

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.

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

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

如果您经常使用数学函数,而pow的三参数版本在Python2.7中很少使用,那么可以通过导入__builtin__并为3参数调用__builtin__.pow来解决这个问题

相关问题 更多 >

    热门问题