FW: [Tutor] printing out the definition of a func [dis/inspect]

alan.gauld@bt.com alan.gauld@bt.com
Mon, 24 Dec 2001 16:19:07 -0000


Oops, meant to hit reply-all...

> -----Original Message-----
> To: 'Kirk Bailey';
> Subject: RE: [Tutor] printing out the definition of a func 
> 
> > Pardon this humble novice, but is the interpeter not a file?
> 
> The interpreter itself is a file. What I meant was the code
> that you type at the Python interactive prompt.
> 
> Thus:
> 
> >>> import inspect
> >>> def foo():
> ...    print 'foo'
> ...
> >>> print inspect.getsource(foo)
> 
> will generate an error. It only works if the code for foo is 
> in a file external to the interpreter.
> 
> Alan G.
> 
> > > BTW It's the inspect module you need, and it only works on
> > > files not the interpreter:
> > > 
> > > >>> import inspect
> > > >>> import foo
> > > >>> print inspect.getsource(foo)
> > > def foo():
> > >   print 'hello'
> > >   return 42
> > > 
> > > def bar():
> > >    print 'bar'
> > >    return 42 * 42
> > > 
> > > >>> print inspect.getsource(foo.foo)
> > > def foo():
> > >   print 'hello'
> > >   return 42
> > > 
> > > >>>
> > > 
> > > Well spotted Danny.
> > > 
> > > Alan G.
> > > 
> > > _______________________________________________
> > > Tutor maillist  -  Tutor@python.org
> > > http://mail.python.org/mailman/listinfo/tutor
> > 
>