这是使用functools.wrap 的另一种方法,它至少在python 3 中保留签名和文档字符串。诀窍是在永远不会被调用的虚拟函数中创建签名和文档。这里有几个例子。
基本示例
import functools
def wrapper(f):
@functools.wraps(f)
def template(common_exposed_arg, *other_args, common_exposed_kwarg=None, **other_kwargs):
print("\ninside template.")
print("common_exposed_arg: ", common_exposed_arg, ", common_exposed_kwarg: ", common_exposed_kwarg)
print("other_args: ", other_args, ", other_kwargs: ", other_kwargs)
return template
@wrapper
def exposed_func_1(common_exposed_arg, other_exposed_arg, common_exposed_kwarg=None):
"""exposed_func_1 docstring: this dummy function exposes the right signature"""
print("this won't get printed")
@wrapper
def exposed_func_2(common_exposed_arg, common_exposed_kwarg=None, other_exposed_kwarg=None):
"""exposed_func_2 docstring"""
pass
exposed_func_1(10, -1, common_exposed_kwarg='one')
exposed_func_2(20, common_exposed_kwarg='two', other_exposed_kwarg='done')
print("\n" + exposed_func_1.__name__)
print(exposed_func_1.__doc__)
结果是:
>> inside template.
>> common_exposed_arg: 10 , common_exposed_kwarg: one
>> other_args: (-1,) , other_kwargs: {}
>>
>> inside template.
>> common_exposed_arg: 20 , common_exposed_kwarg: two
>> other_args: () , other_kwargs: {'other_exposed_kwarg': 'done'}
>>
>> exposed_func_1
>> exposed_func_1 docstring: this dummy function exposes the right signature
调用inspect.signature(exposed_func_1).parameters 返回所需的签名。但是,使用inspect.getfullargspec(exposed_func_1) 仍会返回template 的签名。至少,如果您在 template 的定义中添加任何您想要创建的所有函数共有的参数,它们就会出现。
如果由于某种原因这是一个坏主意,请告诉我!
更复杂的例子
通过在更多的包装器中分层并在内部函数中定义更多不同的行为,您可以得到比这更复杂的东西:
import functools
def wrapper(inner_func, outer_arg, outer_kwarg=None):
def wrapped_func(f):
@functools.wraps(f)
def template(common_exposed_arg, *other_args, common_exposed_kwarg=None, **other_kwargs):
print("\nstart of template.")
print("outer_arg: ", outer_arg, " outer_kwarg: ", outer_kwarg)
inner_arg = outer_arg * 10 + common_exposed_arg
inner_func(inner_arg, *other_args, common_exposed_kwarg=common_exposed_kwarg, **other_kwargs)
print("template done")
return template
return wrapped_func
# Build two examples.
def inner_fcn_1(hidden_arg, exposed_arg, common_exposed_kwarg=None):
print("inner_fcn, hidden_arg: ", hidden_arg, ", exposed_arg: ", exposed_arg, ", common_exposed_kwarg: ", common_exposed_kwarg)
def inner_fcn_2(hidden_arg, common_exposed_kwarg=None, other_exposed_kwarg=None):
print("inner_fcn_2, hidden_arg: ", hidden_arg, ", common_exposed_kwarg: ", common_exposed_kwarg, ", other_exposed_kwarg: ", other_exposed_kwarg)
@wrapper(inner_fcn_1, 1)
def exposed_function_1(common_exposed_arg, other_exposed_arg, common_exposed_kwarg=None):
"""exposed_function_1 docstring: this dummy function exposes the right signature """
print("this won't get printed")
@wrapper(inner_fcn_2, 2, outer_kwarg="outer")
def exposed_function_2(common_exposed_arg, common_exposed_kwarg=None, other_exposed_kwarg=None):
""" exposed_2 doc """
pass
这有点冗长,但关键是在使用它来创建函数时,来自您(程序员)的动态输入的位置有很大的灵活性,因此暴露的输入(来自用户函数)被使用。