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

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

  •  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