首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >返回函数码

返回函数码
EN

Stack Overflow用户
提问于 2012-08-10 13:03:47
回答 1查看 173关注 0票数 1

可能重复: How can I get the source code of a Python function?

如何返回函数的代码?func_name方法按预期工作,但func_code不起作用。

代码语言:javascript
复制
>>> 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>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-08-10 13:08:42

您可以使用

inspect.getsourcelines(function_name)

因为他得到了密码。

代码语言:javascript
复制
In [1]: def testthis(): print "hello"

In [2]: import inspect

In [3]: inspect.getsourcelines(testthis)
Out[3]: ([u'def testthis(): print "hello"\n'], 1)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11902253

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档