How to make an array of functions callable in python

user1477337

I'm trying to create a class A which is basically a list of objects B. I would like to be able to call a method in A which automatically returns a list of the corresponding method in B:

A.method(x) = [B.method(x) for B in A]

The issue is that I need a dynamical behavior, so any method in B is automatically "inherited" by A without having to hard code it.

I have tried using lambda functions and exec("""def ..."""), but nothing seems to work. Here is an attempt:

class A(object):

    def __init__(self,Bs):
        self.listOfBs = Bs[:]

        if self.listOfBs:
            for method_name in dir(self.listOfBs[0]):
                if not callable(getattr(self.listOfBs[0],method_name)):
                    continue

            f = lambda x: [getattr(B,method_name)(x) for B in self.listOfBs]
            setattr(self,method_name,f)

class B(object):
    def __init__(self,name):

        self.name = name

    def getName(self,x):
        return self.name+x

#So if I define:
a = A([B('x'),B('y'),B('z')])

#I would like to have: a.getName('W') = ['xW','yW','zW']
#However I get the error: TypeError: 'str' object is not callable

I think there should be an easy/elegant way of implementing the above behavior in python, but I couldn't find anything that works.

Yoav Glazner

You may use __getattr__ to make method lookup dynamic

class A:
    def __init__(self, bs):
        self.bs = bs

    def __getattr__(self, method_name):
        def call(*args, **kw):
            return [getattr(b, method_name)(*args, **kw) for b in bs]
        return call

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to make python object callable by numpy.array()

Why aren't builtin functions classified as callable in python and how can I make them appear callable in my program?

How can I restrict which functions are callable from an imported module? Can I make a function private? (Python)

Functions, Callable Objects, and how both are created in Python

How to make globally callable python program?

How to create a truly callable array or matrix in Python

How to make a generator callable?

How to make a python module function callable just by importing the module

SWIG how to make a typedef function pointer in a struct callable from Python

how to make class variable callable from another class in python

How to compose a list of functions to a callable

How to make callable attribute in Ruby

How to make an array of objects visible to other functions

How to make a type of chain (array) of functions in typescript?

How to make all combinations of many functions in python?

How to make animation of the evolution of functions (in Python)?

How to call Firebase Callable Functions with HTTP?

object is not callable in function of least_squares, how to make it callable?

PYTHON How to make "associative array"

How to make an array of objects in Python?

How to make a method return type as Callable<Boolean>

How can I make an object callable?

How to make a section of code callable later

In a QMenu, how to make a submenu callable like a QAction

How to make middleware object callable in Django 2.2

How to overcome "classmethod is not callable" in python

How to convert a 'managedbuffer' into a Callable in Python

How to add alternating trigonometric functions to an an array in Python?

How do I make my array available to other functions?