1

I was wondering if there is a way to print what the funciton definition?

so if I want to do something like:

def hello():
    print 'hello'

some_function_to_show_definition(hello())

and the output should be:

print 'hello'

Just messing around in python and I was just wondering :)

EerlijkeDame
  • 477
  • 3
  • 8
  • 18
  • you might want to check [https://github.com/ipython/ipython/blob/313df3800a05f4ef72fb7bf4509ed699926cb1b2/IPython/core/oinspect.py] –  May 03 '14 at 01:48

1 Answers1

3

inspect is the way to go:

In [8]: def foo():
   ...:     print 'hello'
   ...:     

In [9]: import inspect

In [10]: inspect.getsourcelines(foo)
Out[10]: ([u'def foo():\n', u"    print 'hello'\n"], 1)
Grapsus
  • 2,714
  • 15
  • 14