Comment conserver les signatures de fonction originales lors de l'utilisation de décorateurs génériques ?

Barbara Streisand
Libérer: 2024-10-17 16:58:02
original
992 Les gens l'ont consulté

How to Preserve Original Function Signatures when Using Generic Decorators?

Preserving Original Function Signatures When Using Generic Decorators

Problem Overview

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.

Solution 1: Utilizing the decorator Module

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>
Copier après la connexion

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>
Copier après la connexion

Solution 2: Python 3.4+ Enhancements

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>
Copier après la connexion

However, functools.wraps() did not exhibit this behavior in earlier versions of Python.

Other Approaches

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.

Conclusion

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.

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

source:php
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Derniers articles par auteur
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!