有没有办法在python中打印函数的定义

时间:2014-05-02 22:32:26

标签: python function definition

我想知道是否有办法打印功能定义?

所以,如果我想做类似的事情:

def hello():
    print 'hello'

some_function_to_show_definition(hello())

,输出应为:

print 'hello'

只是在python中乱搞,我只是想知道:)

1 个答案:

答案 0 :(得分:3)

inspect是要走的路:

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)
相关问题