jeudi 12 juillet 2018

Calling arbitrary class functions for all members of a list of instances

I am trying to create a wrap for a class class1 (probably, another class) that will have inside a list of several instances of class1, some additional variables, some functions that will operate with this list and also wraps for (nearly) all the functions of class1, that should actually just call the corresponding functions for all the class1 objects stored in the list with the same set of parameters.

Could anyone suggest a better way to do that then just to implement all this functions (that will iterate through a list and call the corresponding functions from _class1_instances) by hand?

Example

class class2:

    __N = []
    List_class1 = []

    def __init__(self, N):
         self.__N = N
         for i in range(0, self.__N): 
             List_class1.append(class1())

    def function1(params)
        for i in range(0, self.__N):
               List_class1[i].function1(params)

    def function2(params)
        ret = []
        for i in range(0, self.__N):
               ret.append(List_class1[i].function2(params))
        return ret

So if a have a number of functions in class1 that I wand to adapt like that, maybe someone can suggest a way to do that wo just rewriting?

Aucun commentaire:

Enregistrer un commentaire