List code of function in interpreter

Peter Hansen peter at engcorp.com
Fri Apr 16 10:07:25 EDT 2004


a9605473 at unet.univie.ac.at wrote:

> A simple question.
> 
> Is it possible in the python (or ipython) interpreter to
> review the source code of a self defined function?

That depends.  The source is actually "lost" in at least
one sense, because Python is compiled to bytecode (similar
in some ways to how Java works) behind the scenes, and the
interpreter executes this bytecode and not the source.

On the other hand, the source file is still right there, usually,
and if you are looking for a programmatic way of finding it
and listing it, I believe the inspect module is what you are
looking for: http://docs.python.org/lib/module-inspect.html

Another and much less suitable (I suspect) option for you is to
disassemble the function using the dis module.

-Peter



More information about the Python-list mailing list