开发者

decorators in python

开发者 https://www.devze.com 2023-04-07 17:54 出处:网络
I am trying to understand the functioning of decorators. What am i doing wrong in the following code. Please do correct it

I am trying to understand the functioning of decorators. What am i doing wrong in the following code. Please do correct it

As I have under开发者_JS百科stood when aFunction() is called it in turn calls myDecorator() which also makes a call to afunction(). Right?

Also how to pass parameters into afunction()

class myDecorator(object):

    def __init__(self, f):
        print "inside myDecorator.__init__()"
        f(1) # Prove that function definition has completed

    def __call__(self):
        print "inside myDecorator.__call__()"

@myDecorator
def aFunction(*a):
    print a
    print "inside aFunction()"

print "Finished decorating aFunction()"

aFunction(2)


class myDecorator(object):

    def __init__(self, f):
        print "inside myDecorator.__init__()"
        # save a reference to the real function, so it can be called later
        self.f = f

    def __call__(self, *args, **kwargs):
        print "inside myDecorator.__call__()"
        # call the real function and return its result
        # passing it any and all arguments
        return self.f(*args, **kwargs)

@myDecorator
def aFunction(*a):
    print a
    print "inside aFunction()"

print "Finished decorating aFunction()"

aFunction(1)

print "Finished calling aFunction()


Your __call__ method is missing the parameter, which you give to aFunction.

class myDecorator(object):

    def __init__(self, f):
        print "inside myDecorator.__init__()"
        f(1) # Prove that function definition has completed
        self.__function = f

    def __call__(self, *args):
        # the *args magic is here to mirror the original parameter list of
        # the decorated function. But it is better to place here parameter list
        # of the function you want to decorate, in order to minimize error possibilities
        print "inside myDecorator.__call__()"
        return self.__function(*args)

@myDecorator
def aFunction(*a):
    print a
    print "inside aFunction()"

print "Finished decorating aFunction()"

aFunction(1)
aFunction('a', 23, 42)


f, in __init__, needs to be saved, then the __call__ method needs to call it. Something like this:

class myDecorator(object):
    def __init__(self, f):
        print "inside myDecorator.__init__()"
        self.f = f
        print "function has been saved"
    def __call__(self, *args):
        print "inside myDecorator.__call__()"
        result = self.f(args)
        print "done with f()"
        return result

@myDecorator
def aFunction(*a):
    print a
    print "inside aFunction()"

aFunction(1)

What happens with decoration is that the original function is replaced with whatever the decorator returns. Your original code, however, was not saving any reference to aFunction so it was getting lost.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号