返回函数cod

2024-04-27 20:02:14 发布

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

Possible Duplicate:
How can I get the source code of a Python function?

如何返回函数的代码? func_name方法按预期工作,但func_代码无法正常工作

>>> def testthis(): print 'test successful'
>>> testthis()
test successful

>>> testthis.func_name
'testthis'
>>> testthis.func_code
<code object testthis at 0124D728, file "<pyshell#281>", line 1>

Tags: ofthe代码nametestsourcegetcode
1条回答
网友
1楼 · 发布于 2024-04-27 20:02:14

你可以用

inspect.getsourcelines(function_name)

因为他得到了密码。在

In [1]: def testthis(): print "hello"

In [2]: import inspect

In [3]: inspect.getsourcelines(testthis)
Out[3]: ([u'def testthis(): print "hello"\n'], 1)

相关问题 更多 >