【发布时间】:2017-01-08 10:12:55
【问题描述】:
我有一个基础装饰器,它接受参数,但也由其他装饰器构建。我似乎无法确定将 functools.wraps 放在哪里以保留装饰函数的完整签名。
import inspect
from functools import wraps
# Base decorator
def _process_arguments(func, *indices):
""" Apply the pre-processing function to each selected parameter """
@wraps(func)
def wrap(f):
@wraps(f)
def wrapped_f(*args):
params = inspect.getargspec(f)[0]
args_out = list()
for ind, arg in enumerate(args):
if ind in indices:
args_out.append(func(arg))
else:
args_out.append(arg)
return f(*args_out)
return wrapped_f
return wrap
# Function that will be used to process each parameter
def double(x):
return x * 2
# Decorator called by end user
def double_selected(*args):
return _process_arguments(double, *args)
# End-user's function
@double_selected(2, 0)
def say_hello(a1, a2, a3):
""" doc string for say_hello """
print('{} {} {}'.format(a1, a2, a3))
say_hello('say', 'hello', 'arguments')
这段代码的结果应该是是:
saysay hello argumentsarguments
但是,在 say_hello 上运行帮助会给我:
say_hello(*args, **kwargs)
doc string for say_hello
除参数名称外,所有内容均被保留。
似乎我只需要在某处添加另一个@wraps(),但是在哪里?
【问题讨论】:
-
你用的是python 2还是python 3?
标签: python python-2.7 python-decorators functools