Skip to content Skip to sidebar Skip to footer

Python Inspect.getargspec With Built-in Function

I'm trying to figure out the arguments of a method retrieved from a module. I found an inspect module with a handy function, getargspec. It works for a function that I define, but

Solution 1:

It is impossible to get this kind of information for a function that is implemented in C instead of Python.

The reason for this is that there is no way to find out what arguments the method accepts except by parsing the (free-form) docstring since arguments are passed in a (somewhat) getarg-like way - i.e. it's impossible to find out what arguments it accepts without actually executing the function.

Solution 2:

You can get the doc string for such functions/methods which nearly always contains the same type of information as getargspec. (I.e. param names, no. of params, optional ones, default values).

In your example

import mathmath.sin.__doc__

Gives

"sin(x)

Return the sine of x (measured in radians)"

Unfortunately there are several different standards in operation. See What is the standard Python docstring format?

You could detect which of the standards is in use, and then grab the info that way. From the above link it looks like pyment could be helpful in doing just that.

Post a Comment for "Python Inspect.getargspec With Built-in Function"