66

This is Python 2.5, and it's GAE too, not that it matters.

I have the following code. I'm decorating the foo() method in bar, using the dec_check class as a decorator.

class dec_check(object):

  def __init__(self, f):
    self.func = f

  def __call__(self):
    print 'In dec_check.__init__()'
    self.func()

class bar(object):

  @dec_check
  def foo(self):
    print 'In bar.foo()'

b = bar()
b.foo()

When executing this I was hoping to see:

In dec_check.__init__()
In bar.foo()

But I'm getting "TypeError: foo() takes exactly 1 argument (0 given)" as .foo(), being an object method, takes self as an argument. I'm guessing problem is that the instance of bar doesn't actually exist when I'm executing the decorator code.

So how do I pass an instance of bar to the decorator class?

ShadowRanger
  • 108,619
  • 9
  • 124
  • 184
Phil
  • 3,038
  • 3
  • 28
  • 39

3 Answers3

93

You need to make the decorator into a descriptor -- either by ensuring its (meta)class has a __get__ method, or, way simpler, by using a decorator function instead of a decorator class (since functions are already descriptors). E.g.:

def dec_check(f):
  def deco(self):
    print 'In deco'
    f(self)
  return deco

class bar(object):
  @dec_check
  def foo(self):
    print 'in bar.foo'

b = bar()
b.foo()

this prints

In deco
in bar.foo

as desired.

ShadowRanger
  • 108,619
  • 9
  • 124
  • 184
Alex Martelli
  • 762,786
  • 156
  • 1,160
  • 1,345
51

Alex's answer suffices when a function is sufficient. However, when you need a class you can make it work by adding the following method to the decorator class.

def __get__(self, obj, objtype):
    """Support instance methods."""
    import functools
    return functools.partial(self.__call__, obj)

To understand this you need to understand the descriptor protocol. The descriptor protocol is the mechanism for binding a thing to an instance. It consists of __get__, __set__ and __delete__, which are called when the thing is got, set or deleted from the instances dictionary.

In this case when the thing is got from the instance we are binding the first argument of its __call__ method to the instance, using partial. This is done automatically for member functions when the class is constructed, but for a synthetic member function like this we need to do it explicitly.

Gordon Wrigley
  • 9,129
  • 8
  • 41
  • 59
  • 6
    @Gilbert The descriptor protocol is the mechanism for binding a thing to an instance. It consists of __ get __, __ set __ and __ delete __, which are called when the thing is got, set or deleted from the instances dictionary. In this case when the thing is got from the instance we are binding the first argument of it's __ call __ method to the instance, using partial. This is done automatically for member functions when the class is constructed, but for a synthetic member function like this we need to do it explicitly. – Gordon Wrigley Feb 03 '11 at 23:35
  • 4
    So what about if the decorator takes arguments? – Adam Parkin Feb 23 '12 at 00:44
  • python docs have a [Descriptor HowTo Guide](https://docs.python.org/2/howto/descriptor.html) section – n611x007 Apr 16 '14 at 19:10
  • More detailed explanation with code: http://stackoverflow.com/questions/5469956/python-decorator-self-is-mixed-up – Youssef G. May 02 '14 at 15:15
  • 1
    A better way to implement `__get__` is to use `types.MethodType` to make an actual bound (or in some cases on Py2, unbound) method object. On Py2, you'd make the `__get__` body `return types.MethodType(self, instance, instancetype)`; on Py3, you'd first test for `None` on `instance` to avoid binding (`if instance is None: return self`), and otherwise, you'd `return types.MethodType(self, instance)`. Full example [here](https://stackoverflow.com/a/57808792/364696). Put the `import` outside the `__get__` though; `import` (even cached) is relatively expensive for simple attribute lookup. – ShadowRanger Sep 05 '19 at 19:53
  • I think your answer should be the one validated because it is more in relation with the context (Using a decorator as a class). Thanks sir! – Nibor Ndj Feb 17 '21 at 18:07
3

If you want to write the decorator as a class you can do:

from functools import update_wrapper, partial

class MyDecorator(object):
    def __init__(self, func):
        update_wrapper(self, func)
        self.func = func

    def __get__(self, obj, objtype):
        """Support instance methods."""
        return partial(self.__call__, obj)

    def __call__(self, obj, *args, **kwargs):
        print('Logic here')
        return self.func(obj, *args, **kwargs)

my_decorator = MyDecorator

class MyClass(object):
     @my_decorator
     def my_method(self):
         pass
user11492607
  • 35
  • 1
  • 9
Thomas Turner
  • 1,806
  • 1
  • 19
  • 19
  • 3
    How would one pass arguments to this decorator? I tried adding as extra argument to the `__init__` method, but then I don't really know what to pass in for `func` when instantiating the decorator: `@my_decorator(func, given_arg)`. – mhyousefi Dec 29 '19 at 07:20
  • @mhyousefi Were you able to find the answer to this? – Ferus May 06 '21 at 15:51
  • @Ferus honestly I don't remember. :) Will update this if I do. – mhyousefi May 08 '21 at 14:46
  • @mhyousefi No problem I actually came up with a solution by adding in the constructor `self.func = decorator()(self.func)` instead of adding a decorator. – Ferus May 08 '21 at 15:11