get function declaration (present in pydoc output) with __doc__ attribute

StackOverflow https://stackoverflow.com/questions/8774878

  •  14-04-2021
  •  | 
  •  

Вопрос

Example:

import numpy
print numpy.polydiv.__doc__

output:

Returns the quotient and remainder of polynomial division...

While using pydoc:

$ pydoc numpy.polydiv

I get:

numpy.polydiv = polydiv(u, v)
Returns the quotient and remainder of polynomial division...

Is there some available attribute that would allow me to get function declaration part polydiv(u, v) by using python script (without using pydoc/terminal)

Это было полезно?

Решение

Just in case you weren't aware, you can always use the help function in an interactive session to do the equivalent of what pydoc does.

But to answer your specific question:

The signature of a function object can be obtained using the inspect module:

>>> import numpy, inspect
>>> def sig(func):
...     argspec = inspect.getargspec(func)
...     return func.__name__ + inspect.formatargspec(*argspec)
... 
>>> print sig(numpy.polydiv)
polydiv(u, v)
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top