Estoy tratando de pasar el nombre de una función a otra función como argumento, pero me sale un error: "TypeError: 'str' object no se puede llamar". Aquí es un ejemplo simplificado del problema:Python: Pasar un nombre de función como argumento en una función
def doIt(a, func, y, z):
result = z
result = func(a, y, result)
return result
def dork1(arg1, arg2, arg3):
thing = (arg1 + arg2)/arg3
return thing
def dork2(arg1, arg2, arg3):
thing = arg1 + (arg2/arg3)
return thing
Cuando llamo doIT así:
var = 'dork1'
ned = doIt(3, var, 4, 9)
print (ned)
me sale:
Traceback (most recent call last):
File "<pyshell#9>", line 1, in <module>
ned = doIt(3, var, 4, 9)
File "<pyshell#2>", line 3, in doIt
result = func(a, y, result)
TypeError: 'str' object is not callable
Rock on! globals() funcionó! Gracias por toda tu ayuda. – Desmond
Noooo! ¡Escucha a Alex Martelli! ¡ESCRIBIÓ a Python en pocas palabras! ¡No recurras a usar un hack feo que rara vez o nunca se necesita para hacer algo que puedes hacer limpiamente! Aprende a usar el idioma correctamente! – jeremiahd