I'm trying to add a wrapper that would add a method to a class that would return the value of the argument passed in to the wrapper, here is what I'd like to achieve:
@addMethod(test='abs')
class a(object):
def method1(cls):
pass
def method2(cls):
pass
def addMethod(*kwargs):
def getTest():
return kwargs.get('test')
return getTest
def main():
x = a()
print x.getTest()
Expected output:
abc
I thought that my wrapper should inherit a new class that has the method, but that doesn't seem correct:
def addMethod(fn):
def wrapped(self, *kwargs):
return fn(AddDependency(kwargs))
return wrapped
class AddDependency(object):
def __init__(self, *kwargs):
super(AddDependency, self).__init__()
self.kwargs = kwargs
def getDependencyScript(self):
return self.kwargs.get('test')
Any thoughts or hints that may lead me the right direction?