Skip to content Skip to sidebar Skip to footer

Is There A Python Equivalent Of R's Str(), Returning Only The Structure Of An Object?

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

Solution 1:

The closest thing in Python would probably be to create a function based off of inspect.getargspec, possibly via inspect.formatargspec.

import inspect

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

This gives you an output like this:

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

Solution 2:

I think you need inspect.getdoc.

Example:

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

And for methods of a 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.

Solution 3:

Yes

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

Post a Comment for "Is There A Python Equivalent Of R's Str(), Returning Only The Structure Of An Object?"