Decorator in Python
I am trying to write this code and I want to it run with self of class. Below is my code:
import typing as tp
def wrapper_f(f: tp.Callable) -> tp.Callable:
def wrapper(self, f: tp.Callable, *args, **kwargs):
return f(self, *args, **kwargs)
return wrapper
class a:
def __init__(self, n):
self.__n__ = n
self.__dict_path__ = {}
@wrapper_f
def add(self, name):
def decorator(f):
self.add_new(name, f)
return f
return decorator
def add_new(self, name, f):
self.__dict_path__[name] = f
def get(self, name):
return self.__dict_path__[name]()
k = a('Hallo')
@k.add('hello')
def help():
return "Hello =)))"
if __name__ == "__main__":
print(k.get('hello'))
And when i run it. And Taddaaa! It not working =((((
>> @k.add('hello')
>> File "D:\Python\Learn\Advanced\decorator2.py", line 4, in wrapper
>> return f(self, *args, **kwargs)
>>TypeError: 'str' object is not callable
Help me please! I don't understand why it not run and how to fix it.
Thank you guys.