def decor(cls):
def wrapper(method):
def wrapped(self, *args, **kwargs):
return self.method(*args, **kwargs)
print method # only a function object here
return wrapped
print cls # here we get the class and can manipulate it
return wrapper
class A
@decor(A)
def method(self):
pass
根据这一回应,这似乎是不可能的:
Get Python function's owning class from decorator
相反,您可以为您的类提供一个decorator,类似于:
我无法想象满足这种要求的方法,因为
decor
函数只接收一个对包含类一无所知的函数对象。在我能想到的唯一解决方法是使用参数化decorator并将被修饰的类传递给它
或者,可以装饰类本身:
^{pr2}$给出:
实际上,您可以装饰类本身:
相关问题 更多 >
编程相关推荐