2010-07-23 10 views
9

¿Por qué Python me dice "TypeError: pow esperaba 2 argumentos, obtuvo 3" a pesar de que funcionaba en IDLE (a veces me dice que también en IDLE)? simplemente estoy haciendo pow(a,b,c). mi programa es muy corto y no cambio la definición de pow en ningún momento ya que necesito usarlo para cierta exponenciación.¿Por qué Python dice que pow solo tiene 2 argumentos?

NOTA: Este es el pow de __builtin__, no Math

Respuesta

14

Incorporado pow toma dos o tres argumentos. Si lo hace from math import *, entonces es reemplazado por pow de matemáticas, que toma solo dos argumentos. Mi recomendación es hacer import math, o enumerar explícitamente las funciones que utiliza en la lista de importación. Un problema similar ocurre con open frente a os.open.

+0

ah ... tal vez sea por eso. ¡¡¡¡¡Gracias!!!!! err ... ¿lo afectaría una importación desde otro archivo? estoy importando otro programa que escribí que también tiene 'from math import *' – calccrypto

+0

@calccrypto: si está importando el otro programa con 'from p import *', entonces sí. Use 'import p' o liste explícitamente' from p import [...] '. – sdcvvc

0

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

Tal vez usted está violando la parte en negrita?

+0

no. Estoy seguro de que todos los valores son enteros positivos editar: sí. a, b, c = 9, 4, 225 – calccrypto

1

Si está utilizando funciones matemáticas mucho y la versión de tres parámetros de pow con poca frecuencia una forma de evitar esto en Python 2.7 es importar __builtin__ y llame __builtin__ .pow para el 3 Paramete

+0

Debería haber dos caracteres de guión bajo a cada lado de 'builitin' en ambos casos, pero el formato lo interpretó como una fuente en negrita: no estoy seguro de qué hacer con thaat. –

Cuestiones relacionadas