[英]function gets function as an argument and throws 'numpy.ndarray' object is not callable
I call a function with other function as an argument. 我用其他函数作为参数调用一个函数。 The other function return numpy.ndarray. 另一个函数返回numpy.ndarray。
The code: 编码:
class CLASS:
def method1(self):
size = 10
return np.zeros([size,size])
def method2(self, method):
res = method()
a = CLASS ()
b = a.method2(a.method1())
The first function throws me TypeError: 'numpy.ndarray' object is not callable 第一个函数抛出TypeError:'numpy.ndarray'对象不可调用
I want to run method2()
in the cycle giving different functions as an argument. 我想在循环中运行method2()
,以提供不同的功能作为参数。
QUESTION : Is it any way to run that in Python 3? 问题 :是否可以在Python 3中运行它?
The a.method1()
returns the result of np.zeros(...)
which is a numpy.ndarray
np.zeros(...)
a.method1()
返回np.zeros(...)
的结果,它是一个numpy.ndarray
So When you're trying to call method()
in method2()
it fails because that's not a function. 因此,当您尝试在method2()
调用method()
,它会失败,因为这不是函数。
You probably want this instead: 您可能想要这样:
import numpy as np
class CLASS:
def method1(self):
size = 10
return np.zeros([size,size])
def method2(self, glcm):
pass
a = CLASS ()
b = a.method2(a.method1())
It seems like you are passing the result of calling method1
(which is in fact a numpy.ndarray
) into method2
rather than the method itself. 似乎您正在将调用 method1
的结果 (实际上是numpy.ndarray
) numpy.ndarray
给method2
而不是方法本身。
The call at the end should be a.method2(a.method1)
without the parens. 最后的调用应该是没有括号的a.method2(a.method1)
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.