Skip to content

Decorator example

py
import inspect
import functools

def my_decorator(self):
    @functools.wrap(func)
    def wrapper(self, *args, **kwargs):
        function_args_keys = (*inspect.getfullargspec(func).args)
        function_args = {k: v for k, v in kwargs.items() if k in function_args_keys}
        self.__dict__.update(kwargs.items()) # merge dict k/v into field/value in self
        return func(self, *args, **function_args) # execute next func
    return wrapper 

@my_decorator
def my_func(self):
    print("aa")