I have been experimenting with some decorators and how they are inherited. The thing I am trying to do is create a decorator that can be inherited by another class that will add the method to a dictionary within this class.
class A:
def __init__(self):
self.mydict = {}
def Appenddict(key):
def _Appenddict(function):
# I want to add to the dictionary here so i dont have to call the method
def wrapper(*args):
self = args[0]
self.mydict[key] = function
return function
return wrapper
return _Appenddict
class B(A):
def __init__(self):
A.__init__(self)
@A.Appenddict("Key1")
def MyMethod(self):
print("My Method")
B1 = B()
print(B1.mydict)
B1.MyMethod()
print(B1.mydict)
The code above shows my attempt at creating such a system. It is capable of achieving this, but it requires calling the method, which is something I do not want. Any ideas?
New contributor
MJ12 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.