I would have add method to a class dynamically... function name will also passed dynamically.

How can i do? I tried in this way

def decor(*var):
  def onDecorator(aClass):
    class onInstance:
        def __init__(self,*args,**kargs):
            setter=var
            aClass.setter = self.flam
            self.wrapped = aClass(*args,**kargs)

        def __getattr__(self,attr):
            return getattr(self.wrapped,attr)

        def __setattr__(self,attr,value):
            if attr == 'wrapped':
                self.__dict__[attr]=value
            else:
                setattr(self.wrapped,attr,value)

        def flam(self,*args):
            self.__setattr__('dimension',len(args[0]))

    return onInstance
return onDecorator

but if i do:

print(aClass.__dict__)

i have

'setter': <bound method onInstance.flam of <__main__.onInstance object at 0x522270>>

instead of var:.....

i have this class:

class D:
  def __init__(self, data):
    self.data = data
    self.dimension = len(self.data)

i would call:

D.name()

and have back self.dimension but i don't know name in advance

Edit
Report