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

In Python, help(functionName)they functionName?return all the documentation for a given function, which often has too much text on the command line. Is there a way to return only input parameters?

R str()does this, and I use it all the time.

+3
source share
3 answers

The closest thing in Python would probably be to create a function based on inspect.getargspec, perhaps, through inspect.formatargspec.

import inspect

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

This will give you the following view:

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

I think you need to inspect.getdoc.

Example:

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

And for class methods:

>> 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.
+2
source

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
0

Source: https://habr.com/ru/post/1615853/


All Articles