0

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Hani Zion
  • 11
  • 4
  • 1
    Note that a decorator with arguments is applied as follows: `thing = decorator(args)(thing)`. Also one `*` is for positional, not keyword, arguments and the first argument to an instance method should be `self`. – jonrsharpe May 25 '17 at 21:53
  • 1
    The cogent issues have been addressed already by Jon. You can find a working edit to your code here: https://repl.it/ISOk/0 – Moses Koledoye May 25 '17 at 22:08

0 Answers0