Accessing bound method or self when decorating a method

849 Views Asked by At

I have a use case where I want to decorate a method with an additional way to call it, such as in this code:

def decorator(func):
    def enhanced(*args, **kwargs):
        func(*args, **kwargs)

    func.enhanced = enhanced
    return func

@decorator
def function():
    pass

class X:
    @decorator
    def function(self):
        pass

x = X()

function()
function.enhanced()
x.function()
# x.function.enhanced()
x.function.enhanced(x)

The first three calls work as expected, but x.function.enhanced() does not; I'd have to write x.function.enhanced(x) to make it work. I know that this is because the func passed to the decorator is not a bound method but a function, and thus needs to be passed self explicitly.

But how can I get around this? From the little bit I understand about descriptors, they're only relevant when looking up on a class, and as func is not a class, func.enhanced is not looked up in a way that I can intercept.

Is there something I can do here?

2

There are 2 best solutions below

8
On BEST ANSWER

You can return a descriptor that returns an object that makes itself callable and has an enhanced attribute mapped to your enhanced wrapper function:

from functools import partial
def decorator(func):
    class EnhancedProperty:
        # this allows function.enhanced() to work
        def enhanced(self, *args, **kwargs):
            print('enhanced', end=' ') # this output is for the demo below only
            return func(*args, **kwargs)
        # this allows function() to work
        def __call__(self, *args, **kwargs):
            return func(*args, **kwargs)
        def __get__(self, obj, objtype):
            class Enhanced:
                # this allows x.function() to work
                __call__ = partial(func, obj)
                # this allows x.function.enhanced() to work
                enhanced = partial(self.enhanced, obj)
            return Enhanced()
    return EnhancedProperty()

so that:

@decorator
def function():
    print('function')

class X:
    @decorator
    def function(self):
        print('method of %s' % self.__class__.__name__)

x = X()

function()
function.enhanced()
x.function()
x.function.enhanced()

would output:

function
enhanced function
method of X
enhanced method of X
1
On

Just as an example of what I mean in the comments to the answer posted by @blhsing :

class EnhancedProperty:
    def __init__(self, func):
        self.func = func
    def enhanced(self, *args, **kwargs):
        return self.func(*args, **kwargs)
    def __call__(self, *args, **kwargs):
        return self.func(*args, **kwargs)
    def __get__(self, obj, typ):
        return Enhanced(self.func, obj, typ)

class Enhanced:
    def __init__(self, func, obj, typ):
        self.func = func
        self.obj = obj
        self.typ = typ
    def __call__(self, *args, **kwargs):
        return self.func.__get__(self.obj, self.typ)(*args, **kwargs)
    def enhanced(self, *args, **kwargs):
        return self.func(self.obj, *args, **kwargs)

def decorator(f):
    return EnhancedProperty(f)

In the REPL:

In [2]: foo(8, -8)
Out[2]: 1040

In [3]: foo.enhanced(8, -8)
Out[3]: 1040

In [4]: Bar().baz('foo')
Out[4]: ('foo', 'foo')

In [5]: Bar().baz.enhanced('foo')
Out[5]: ('foo', 'foo')