gpt4 book ai didi

python - 是否存在与 R 的 str() 等效的 Python,仅返回对象的结构?

转载 作者:太空宇宙 更新时间:2023-11-03 13:16:38 25 4
gpt4 key购买 nike

在 Python 中,help(functionName)functionName? 返回给定函数的所有文档,命令行中的文本通常太多。有没有办法只返回输入参数?

R 的 str() 就是这样做的,我一直都在使用它。

最佳答案

Python 中最接近的可能是创建一个基于 inspect.getargspec 的函数, 可能通过 inspect.formatargspec .

import inspect

def rstr(func):
return inspect.formatargspec(*inspect.getargspec(func))

这会给你这样的输出:

>>> def foo(a, b=1): pass
...
>>> rstr(foo)
'(a, b=1)'

关于python - 是否存在与 R 的 str() 等效的 Python,仅返回对象的结构?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27749573/

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