简体   繁体   English

函数获取函数作为参数并抛出“ numpy.ndarray”对象不可调用

[英]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.ndarraynumpy.ndarraymethod2而不是方法本身。

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.

相关问题 在最小化中使用CALLABLE函数时,'numpy.ndarray'对象不可调用 - 'numpy.ndarray' object is not callable when using a CALLABLE function in minimization 调用 function 作为 minimum_squares 中的参数返回“ TypeError: 'numpy.ndarray' object is not callable ” - call a function as argument in least_squares return “ TypeError: 'numpy.ndarray' object is not callable ” 初始化函数时出现“ numpy.ndarray是对象不可调用”错误 - “ numpy.ndarray is object is not callable ” error in initiating a function “'numpy.ndarray'对象不可调用” - “'numpy.ndarray' object is not callable” 'numpy.ndarray'对象不可调用 - 'numpy.ndarray' object is not callable 在查找 Sigmoid function 的导数时出现错误(numpy.ndarray 不可调用) - Getting error (numpy.ndarray not callable) in Finding derivative of Sigmoid function Python:numpy.ndarray对象不可调用,查找根函数错误 - Python: numpy.ndarray object not callable error for root-finding function 'numpy.ndarray':对象不可调用错误 - 'numpy.ndarray' : object is not callable error 'numpy.ndarray' 对象不可调用,代码如下 - 'numpy.ndarray' object is not callable, code is as follows scipy最小化'numpy.ndarray'对象不可调用 - scipy minimize 'numpy.ndarray' object is not callable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM