是否有一个Python等价的R的str(),只返回一个对象的结构?


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)'
点赞