0

I'm writing a decorator that I'll use on a class Foo method. Something like this:

class decorator(object):
    def __init__(self,func):
        self.func = func

    def __call__(self,*args,**kwargs):
        result = self.func(*args,**kwargs)
        # Do stuff
        return result

class Foo(object):
    def __init__(self,arg):
        self.arg = arg

    def load(cls,cfg):
        # construct object
        return cls(arg)

    @decorator      
    def bar(self,x):
        # run method
        return self

When I run the code, it says TypeError: bar() takes exactly 2 arguments (1 given)

The method is called like this:

def process(cls,cfg,x):
    obj = cls.load(cfg)
        if obj:
            return obj.bar(x)

However, if I define the decorator using functions, it seems to work.

What am I doing wrong?

cap
  • 365
  • 2
  • 14

0 Answers0