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

alan.gauld@bt.com alan.gauld@bt.com
Mon, 24 Dec 2001 11:47:49 -0000

> Wait, wait, I spoke too soon.  It is possible!  Take a look at:
>     http://www.python.org/doc/lib/inspect-source.html

Version 2.1 onwards unfortunately but OTOH the fact it's 
possible is amazing. Python blows me away once more.

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.