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)
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top