Decorators can enhance functions with additional functionality, but generic decorators often replace the original function's signature with a wildcard signature like "args, *kwargs." This can be problematic, especially when generating documentation or introspecting function metadata.
The decorator module provides an easy solution. By wrapping the decorator function with decorator.decorator, we can preserve the original function's signature:
<code class="python">import decorator @decorator.decorator def args_as_ints(f, *args, **kwargs): args = [int(x) for x in args] kwargs = dict((k, int(v)) for k, v in kwargs.items()) return f(*args, **kwargs) @args_as_ints def funny_function(x, y, z=3): """Computes x*y + 2*z""" return x*y + 2*z</code>
This approach maintains the original function's signature:
<code class="python">help(funny_function) # Help on function funny_function in module __main__: # # funny_function(x, y, z=3) # Computes x*y + 2*z</code>
In Python 3.4 and later, functools.wraps() automatically preserves the original function signature:
<code class="python">import functools def args_as_ints(func): @functools.wraps(func) def wrapper(*args, **kwargs): args = [int(x) for x in args] kwargs = dict((k, int(v)) for k, v in kwargs.items()) return func(*args, **kwargs) return wrapper @args_as_ints def funny_function(x, y, z=3): """Computes x*y + 2*z""" return x*y + 2*z help(funny_function) # Help on function funny_function in module __main__: # # funny_function(x, y, z=3) # Computes x*y + 2*z</code>
However, functools.wraps() did not exhibit this behavior in earlier versions of Python.
Duplicating the signature in the function's docstring or creating custom decorators for each specific signature are flawed workarounds that introduce duplication and maintenance issues.
By utilizing the decorator module or taking advantage of functools.wraps() in Python 3.4+, decorators can be used generically while preserving the original function signatures, ensuring robust documentation and introspection capabilities.
위 내용은 일반 데코레이터를 사용할 때 원래 함수 서명을 보존하는 방법은 무엇입니까?의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!