gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 23:24:27 24 4
gpt4 key购买 nike

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

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

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com