How to see the code definiton in the shell ?

CTO debatem1 at gmail.com
Thu May 14 01:22:23 CEST 2009


On May 13, 1:26 pm, "J. Cliff Dyer" <j... at sdf.lonestar.org> wrote:
> On Wed, 2009-05-13 at 09:40 -0700, Mohan Parthasarathy wrote:
> > Hi,
>
> > I am new to Python. I tried searching this but could not find an
> > answer. In the interactive shell, I write a new function and I want to
> > be able to see all the code that I wrote at a later time. Just typing
> > the function name only shows
>
> > >>> allmethods
> > <function allmethods at 0x822b0>
>
> > How do I see the actual code ?
>
> > thanks
> > mohan

If you save your function to a file you can. Saying you have the
following
file:

----------------------------
#! /usr/bin/env python3

def f(x, y):
    return x**y

----------------------------

saved as f.py, you can go into the interpreter and do:

>>> import inspect
>>> from f import f
>>> inspect.getsource(f)
'def f(x, y):\n\treturn x**y\n'

However, trying this on a function written in the interpreter
will bail on you.

Geremy Condra



More information about the Python-list mailing list