Skip to main content
 首页 » 编程设计

python之kwargs 和函数签名

2025年05月04日10pander-it

有没有办法为带有 **kwargs 的函数提供签名引用(例如,用于 IDE)?

例如:

def foo(**kwargs): 
  # call some third-party library with kwargs signature 

我想为最新的 IDE 提供自动完成功能

那么,我该如何在 Python 中描述签名

请您参考如下方法:

这不是您想要的,但它可能...帮助。

from other_package import my_function 
 
 
def wrapped_help_text(wrapped_func): 
    """Decorator to pass through the documentation from a wrapped function. 
    """ 
    def decorator(wrapper_func): 
        """The decorator. 
 
        Parameters 
        ---------- 
        wrapped_func : callable 
            The wrapped function. 
 
        """ 
        wrapper_func.__doc__ = ('This method wraps the following method:\n\n' + 
                                pydoc.text.document(wrapped_func)) 
        return wrapper_func 
    return decorator 
 
 
@wrapped_help_text(my_function) 
def wrapper(**kwargs): 
    """ 
    Parameters 
    ---------- 
    **kwargs 
        See other_package.my_function() 
 
    """ 
    my_function(**kwargs) 

有了它,您现在可以调用内置的 help 函数来查看包装函数的文档字符串。