Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Decorator with arguments and accessing class instance [duplicate]

I have a class defined as follows:

class SomeViewController(BaseViewController):
    @requires('id', 'param1', 'param2')
    @ajaxGet
    def create(self):
        #do something here

Is it possible to write a decorator function that:

  1. Takes a list of args, and possibly kwargs, and
  2. Accesses the instance of the class the method its decorating is defined in?

So for the @ajaxGet decorator, there is a attribute in self called type which contains the value I need to check.

Thanks

like image 589
Michael Bates Avatar asked Mar 04 '13 06:03

Michael Bates


1 Answers

Yes. In fact, in the sense you seem to mean, there isn't really a way to write a decorator that doesn't have access to self. The decorated function wraps the original function, so it has to accept at least the arguments that that function accepts (or some arguments from which those can be derived), otherwise it couldn't pass the right arguments to the underlying function.

There is nothing special you need to do to do this, just write an ordinary decorator:

def deco(func):
    def wrapper(self, *args, **kwargs):
        print "I am the decorator, I know that self is", self, "and I can do whatever I want with it!"
        print "I also got other args:", args, kwargs
        func(self)
    return wrapper

class Foo(object):
    @deco
    def meth(self):
        print "I am the method, my self is", self

Then you can just use it:

>>> f = Foo()
>>> f.meth()
I am the decorator, I know that self is <__main__.Foo object at 0x0000000002BCBE80> and I can do whatever I want with it!
I also got other args: () {}
I am the method, my self is <__main__.Foo object at 0x0000000002BCBE80>
>>> f.meth('blah', stuff='crud')
I am the decorator, I know that self is <__main__.Foo object at 0x0000000002BCBE80> and I can do whatever I want with it!
I also got other args: (u'blah',) {'stuff': u'crud'}
I am the method, my self is <__main__.Foo object at 0x0000000002BCBE80>
like image 156
BrenBarn Avatar answered Oct 17 '22 07:10

BrenBarn