Display Function Code Body?

Steven Bethard steven.bethard at gmail.com
Fri Jan 7 15:49:15 EST 2005


Haibao Tang wrote:
> What I would like to do is to write a function like disp(), when typed,
> it can give you the code infomation.

Check the docs entitled "Retrieving source code":

http://docs.python.org/lib/inspect-source.html

Depending on what you want, you may be able to use inspect.getsource:

py> import inspect
py> import string
py> print inspect.getsource(string.split)
def split(s, sep=None, maxsplit=-1):
     """split(s [,sep [,maxsplit]]) -> list of strings

     Return a list of the words in the string s, using sep as the
     delimiter string.  If maxsplit is given, splits at no more than
     maxsplit places (resulting in at most maxsplit+1 words).  If sep
     is not specified or is None, any whitespace string is a separator.

     (split and splitfields are synonymous)

     """
     return s.split(sep, maxsplit)

However, this won't work for functions you've defined interactively I 
don't think.  On the other hand, if you defined them interactively, you 
can just scroll up. ;)

Steve



More information about the Python-list mailing list