Alex's answer is enough when the function is enough. 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. A descriptor protocol is a mechanism for binding an object to an instance. It consists of __ get __, __set __ and __ delete __, which are called upon receipt, installation or removal of an object from the instance dictionary.
In this case, when the thing is obtained from the instance, we bind the first argument of its __call__ method to the instance using partial. This is done automatically for member functions when a class is created, but for a synthetic member function like this, we need to do this explicitly.
Gordon Wrigley Jul 21 '10 at 4:28 2010-07-21 04:28
source share