Hailiang Zhang
Hailiang Zhang

Reputation: 18870

In python, how to check the function form of an imported module method

For example, I imported Tkinter, and want to check how Tkinter.Frame.__init__ parameter list was defined, and possibly, what is inside the function. I am expecting something like Tkinter.Frame.__init__.__doc__.

(I am not using Python IDE, so the parameter list won't pop up)

Upvotes: 2

Views: 113

Answers (1)

MattH
MattH

Reputation: 38247

I suggest you have a look at inspect.getargspec and inspect.getsource:

>>> import inspect

>>> inspect.getargspec(inspect.getargspec)
ArgSpec(args=['func'], varargs=None, keywords=None, defaults=None)
>>> print inspect.getsource(inspect.getsource)
def getsource(object):
    """Return the text of the source code for an object.

    The argument may be a module, class, method, function, traceback, frame,
    or code object.  The source code is returned as a single string.  An
    IOError is raised if the source code cannot be retrieved."""
    lines, lnum = getsourcelines(object)
    return string.join(lines, '')

Upvotes: 3

Related Questions