首页  >  文章  >  后端开发  >  使用通用装饰器时如何保留原始函数签名?

使用通用装饰器时如何保留原始函数签名?

Barbara Streisand
Barbara Streisand原创
2024-10-17 16:58:02995浏览

How to Preserve Original Function Signatures when Using Generic Decorators?

使用通用装饰器时保留原始函数签名

问题概述

装饰器可以通过附加功能来增强函数,但通用装饰器通常会取代原始函数的签名带有通配符签名的签名,例如“args,*kwargs”。这可能会出现问题,尤其是在生成文档或内省函数元数据时。

解决方案 1:利用装饰器模块

装饰器模块提供了一个简单的解决方案。通过用decorator.decorator包装装饰器函数,我们可以保留原始函数的签名:

<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>

这种方法保留了原始函数的签名:

<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>

解决方案2:Python 3.4增强

在 Python 3.4 及更高版本中,functools.wraps() 自动保留原始函数签名:

<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>

但是,functools.wraps() 在早期版本的 Python 中并未表现出此行为.

其他方法

在函数的文档字符串中复制签名或为每个特定签名创建自定义装饰器都是有缺陷的解决方法,会引入重复和维护问题。

结论

通过利用装饰器模块或利用 Python 3.4 中的 functools.wraps() ,可以通用地使用装饰器,同时保留原始函数签名,确保强大的文档和自省功能。

以上是使用通用装饰器时如何保留原始函数签名?的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn