gpt4 book ai didi

python - kwargs 和函数签名

转载 作者:太空宇宙 更新时间:2023-11-04 05:37:57 25 4
gpt4 key购买 nike

有没有办法为带有 **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 函数来查看包装函数的文档字符串。

关于python - kwargs 和函数签名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35022086/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com