简体   繁体   中英

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.

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

I want to run method2() in the cycle giving different functions as an argument.

QUESTION : Is it any way to run that in Python 3?

The a.method1() returns the result of np.zeros(...) which is a numpy.ndarray

So When you're trying to call method() in method2() it fails because that's not a function.

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.

The call at the end should be a.method2(a.method1) without the parens.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM