简体   繁体   中英

Decorator function using *args and **kargs as parameters for its __call__ method,

I'm currently working with a decorated function that is supposed to check a function's annotations if a certain bool is set to True in the decorator class. What I'm a bit confused about is the decorator function's __call__ method header:

def __call__(self, *args, **kargs):

and its parameter list.

I understand that its parameters *args and **kargs are used for the arguments provided to the function, but in the problem I'm dealing with I have to call the original function (which is an attribute in the instance object created from the decorator class named _f ).

How would I go about calling _f with the arguments provided to the decorated function? Should I check to see whether args / kargs is empty and call self._f(args) / self._f(kargs) for the nonempty parameter?

When you decorate the method:

class Foo:

    @someDecorator
    def my_method(self, x, y):
        ...

It isn't yet actually a method; it's just a function. Inside your decorator, you treat it as such:

def someDecorator(f):
    def _(*args, **kwargs):
        ...
        return f(*args, **kwargs)
    return _

When _ gets called, self will be included in the *args argument.

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