[英]Python3 impossible to pass @property as decorator argument
我已经实现了可以接收额外的 arguments 并希望将其与 class 方法一起使用的装饰器。 我想将@property 作为装饰器参数传递,但我得到的不是@property 结果:
<property object at 0x7f50f5195230>
这是我的装饰器:
class Decorator(object):
def __init__(self, some_arg):
self.func = None
self.some_arg = some_arg
def __get__(self, instance, owner):
import functools
return functools.partial(self.__call__, instance)
def __call__(self, func):
self.func = func
def wrapper(*args, **kwargs):
return self._process_sync(*args, **kwargs)
return wrapper
def _process_sync(self, *args, **kwargs):
try:
print(self.some_arg)
return self.func(*args, **kwargs)
except Exception as e:
print(e)
return None
我的测试 class:
class Test(object):
@property
def some_data(self):
return {'key': 'value'}
@Decorator(some_data)
def some_method(self):
print('method output')
return None
用法:
test = Test()
test.some_method()
两个问题:
<property object at 0x7f50f5195230>
property
object 是一个描述符。 要从中获取值,您需要使用适当的实例调用其__get__
方法。 在您当前的代码中确定何时执行此操作并不容易,因为您的Decorator
object 有很多不同的角色。 它既是装饰器工厂(使用@Decorator(x)
行中的参数进行初始化),又是装饰器本身(使用 function 进行装饰)。 你给了它一个__get__
方法,但我不希望它被使用,因为Decorator
的实例永远不会被分配给 class 变量(只有从__call__
返回的包装器 function )。
无论如何,这是一个修改后的版本,其中Decorator
处理了描述符协议本身的几乎所有部分:
class Decorator:
def __init__(self, arg):
self.arg = arg # this might be a descriptor, like a property or unbound method
def __call__(self, func):
self.func = func
return self # we still want to be the descriptor in the class
def __get__(self, instance, owner):
try:
arg = self.arg.__get__(instance, owner) # try to bind the arg to the instance
except AttributeError: # if it doesn't work, self.arg is not a descriptor, that's OK
arg = self.arg
def wrapper(*args, **kwargs): # this is our version of a bound method object
print(arg) # do something with the bound arg here
return self.func.__get__(instance, owner)(*args, **kwargs)
return wrapper
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.