是否有 Python 等效于 R 的 str(),仅返回对象的结构?

Is there a Python equivalent of R's str(), returning only the structure of an object?

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

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

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

import inspect

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

这会给你这样的输出:

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

我认为你需要 inspect.getdoc

示例:

>> print inspect.getdoc(list)
list() -> new empty list
list(iterable) -> new list initialized from iterable's items

对于 class 的方法:

>> print inspect.getdoc(str.replace)
S.replace(old, new[, count]) -> string

Return a copy of string S with all occurrences of substring  
old replaced by new.  If the optional argument count is
given, only the first count occurrences are replaced.

from sklearn.datasets import load_iris
import pandas as pd
data = load_iris()
df = pd.DataFrame(data.data, columns=data.feature_names)
df.info()

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 150 entries, 0 to 149
Data columns (total 4 columns):
sepal length (cm)    150 non-null float64
sepal width (cm)     150 non-null float64
petal length (cm)    150 non-null float64
petal width (cm)     150 non-null float64
dtypes: float64(4)
memory usage: 4.8 KB