[IPython-user] %psource

Fernando Perez fperez.net at gmail.com
Mon Jan 8 16:31:52 CST 2007


On 1/8/07, Flavio Coelho <fccoelho at gmail.com> wrote:
> Hi,
>
> I just found out about this great magic function, %psource
>
> how does it work? if I want to use that functionality in a script of
> mine how do I do it?

It's a bit tied into ipython, but at the end of the day it's just a
light wrapper around facilities in the inspect module.  You can see
all the code for the IPython introspection machinery in
IPython/OInspect.py.  You'll see that it's basically wrappers on top
of inspect.py, with extra formatting and namespace analysis.

> I also noted that if  I define a function:
>
> def fun():
>     print 'hi'
>
> and then do
>
> %psource fun
>
> I get:
> No source found for fun
>
> what is happening here?

That interactively defined functions don't store their source
anywhere.  The compiler discards the source string after it compiles
the function object, so there is nowhere to go looking for the code,
it's just gone.

There have been discussions in python-dev about attaching the source
string to the function objects themselves, but they've (as far as I
know) always decided against.

best,

f


More information about the IPython-user mailing list