code of a function

Christian Heimes lists at cheimes.de
Thu May 29 06:05:55 EDT 2008


Dark Wind schrieb:
> Hi,
> 
> Is there any command in Python which gives the code for a function like just
> typing the name of a function (say svd) in R returns its code.

Yes, it's posible to retrieve the source code IFF the function is
implemented in Python and the .py file is available, too.

>>> 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, '')




More information about the Python-list mailing list