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)
题
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 :)
解决方案
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)