装饰方法时访问绑定方法或 self

Sil*_*eak 5 python python-decorators python-descriptors

我有一个用例,我想用一种额外的调用方法来装饰一个方法,例如在以下代码中:

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)
Run Code Online (Sandbox Code Playgroud)

前三个调用按预期工作,但x.function.enhanced()没有;我必须写信x.function.enhanced(x)才能让它发挥作用。我知道这是因为func传递给装饰器的不是绑定方法而是函数,因此需要传递self显式传递。

但我该如何解决这个问题呢?从我对描述符的一点了解来看,它们仅在查找类时才相关,而func不是类,func.enhanced不会以我可以拦截的方式查找。

我在这里可以做些什么吗?

blh*_*ing 4

您可以返回一个描述符,该描述符返回一个使其自身可调用的对象,并且具有enhanced映射到enhanced包装函数的属性:

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()
Run Code Online (Sandbox Code Playgroud)

以便:

@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()
Run Code Online (Sandbox Code Playgroud)

会输出:

function
enhanced function
method of X
enhanced method of X
Run Code Online (Sandbox Code Playgroud)