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

2024-04-23 19:04:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我想知道有没有办法打印出函数的定义?你知道吗

所以如果我想做这样的事情:

def hello():
    print 'hello'

some_function_to_show_definition(hello())

输出应为:

print 'hello'

我只是在玩python,我只是在想:)


Tags: to函数hello定义defshowfunctionsome
1条回答
网友
1楼 · 发布于 2024-04-23 19:04:44

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)

相关问题 更多 >