是否有与R'相当的Python;s str(),仅返回对象的结构?

是否有与R'相当的Python;s str(),仅返回对象的结构?,python,Python,在Python中,help(functionName)和functionName?返回给定函数的所有文档,这通常是命令行中的太多文本。有没有办法只返回输入参数 R的str()就是这样做的,我一直都在使用它 Python中最接近的事情可能是基于的创建函数,可能是通过 这将为您提供如下输出: >>> def foo(a, b=1): pass ... >>> rstr(foo) '(a, b=1)' 我想你需要 例如: >> print inspec

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


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

Python中最接近的事情可能是基于的创建函数,可能是通过

这将为您提供如下输出:

>>> def foo(a, b=1): pass
...
>>> rstr(foo)
'(a, b=1)'
我想你需要

例如:

>> print inspect.getdoc(list)
list() -> new empty list
list(iterable) -> new list initialized from iterable's items
对于类的方法:

>> 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.

从sklearn.dataset导入加载
作为pd进口熊猫
数据=加载
df=pd.DataFrame(data.data,columns=data.feature\u名称)
df.info()
范围索引:150个条目,0到149
数据列(共4列):
萼片长度(厘米)150非空花萼64
萼片宽度(厘米)150非空花色64
花瓣长度(厘米)150非空浮点数64
花瓣宽度(厘米)150非空浮动64
数据类型:float64(4)
内存使用率:4.8KB

inspect.getdoc()
返回与
help()
相同的信息,OP已经声明的信息有时太多了。@Amber:Oops,你是对的。我被误导了,因为
help(list)
返回了全班的文档。这回答了OP文章正文中的问题,但不是标题中的问题。对于大多数对象类型,这不起作用。。类似于
{a:getattr(model,a)的函数用于in dir(model),如果不是a.startswith('''''u'')}
的函数确实适用于大多数对象,但是它非常不可读,并且不像R的
str()
那样递归。
>> 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