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

标签 python function definition

我想知道是否有办法打印函数定义?

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

def hello():
    print 'hello'

some_function_to_show_definition(hello())

输出应该是:

print 'hello'

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

最佳答案

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)

关于python - 有没有办法在Python中打印函数的定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23437914/

相关文章:

sql-server - SQL - 审计日志表设计 - 你更喜欢哪个?

python - 转换为 numpy 后如何 reshape pandas 系列?

python - Numpy 广播数组

python - 使用 Python 读取 .xlsx 文件的最快方法

使用非默认选项运行现有函数

c - 定义大小的char数组指针

python - 使用设置目录时,我应该将其导入到哪里?

C 帮助 : Compiler gives me a "makes pointer from integer without cast" when using strlen()

python - NameError:方向未定义?

c++ - 类外的成员函数定义