python 在交互式 shell 中显示函数定义
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2193231/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Display function definition in interactive shell
提问by Count Boxer
I am working in the Python Interactive Shell (ActiveState ActivePython 2.6.4 under Windows XP). I created a function that does what I want. However, I've cleared the screen so I can't go back and look at the function definition. It is also a multiline function so the up arrow to redisplay lines is of minimal value. Is there anyway to return the actual code of the function? There are "code" and "func_code" attributes displayed by dir(), but I don't know if they contain what I need.
我在 Python Interactive Shell(Windows XP 下的 ActiveState ActivePython 2.6.4)中工作。我创建了一个可以执行我想要的功能的函数。但是,我已经清除了屏幕,因此无法返回查看函数定义。它也是一个多行函数,所以重新显示行的向上箭头是最小的值。反正有没有返回函数的实际代码?dir() 显示了“ code”和“func_code”属性,但我不知道它们是否包含我需要的内容。
采纳答案by Alex Martelli
No, __code__
and func_code
are references to the compiled bytecode -- you can disassemble them (see dis.dis
) but not get back to the Python source code.
不,__code__
并且func_code
是对编译后的字节码的引用 —— 您可以反汇编它们(参见 参考资料dis.dis
),但不能返回到 Python 源代码。
Alas, the source code is simply gone, not remembered anywhere...:
唉,源代码根本就不见了,在任何地方都不记得......:
>>> import inspect
>>> def f():
... print 'ciao'
...
>>> inspect.getsource(f)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/inspect.py", line 694, in getsource
lines, lnum = getsourcelines(object)
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/inspect.py", line 683, in getsourcelines
lines, lnum = findsource(object)
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/inspect.py", line 531, in findsource
raise IOError('could not get source code')
IOError: could not get source code
>>>
If inspect
can't get to it, that's a pretty indicative sign.
如果inspect
不能达到它,这是一个非常指示性的标志。
If you were on a platform using GNU readline
(basically, any exceptWindows), you might exploit the fact that readline
itself doesremember some "history" and can write it out to a file...:
如果您在使用 GNU 的平台上readline
(基本上,除Windows之外的任何平台),您可能会利用这样一个事实,即readline
它本身确实记住了一些“历史”并且可以将其写出到文件中...:
>>> readline.write_history_file('/tmp/hist.txt')
and then read that history file -- however, I know of no way to do this in Windows.
然后阅读那个历史文件——但是,我知道在 Windows 中无法做到这一点。
You may want to use some IDE with better memory capabilities, rather than the "raw" command interpreter, especially on a platform such as Windows.
您可能希望使用一些具有更好内存容量的 IDE,而不是“原始”命令解释器,尤其是在 Windows 等平台上。
回答by Mesut GUNES
It has been a pretty long time but may someone need it. getsource
gives the source code:
这已经很长时间了,但可能有人需要它。getsource
给出源代码:
>>> def sum(a, b, c):
... # sum of three number
... return a+b+c
...
>>> from dill.source import getsource
>>>
>>> getsource(sum)
'def sum(a, b, c):\n # sum of three number\n return a+b+c\n'
to install dill
run pip install dill
. To get it formatted:
安装dill
运行pip install dill
。要格式化:
>>> fun = getsource(sum).replace("\t", " ").split("\n")
>>> for line in fun:
... print line
...
def sum(a, b, c):
# sum of three number
return a+b+c
回答by Cerin
No, not really. You could write yourfunc.func_code.co_code (the actually compiled bytecode) to a file and then try using decompyle or unpyc to decompile them, but both projects are old and unmaintained, and have never supported decompiling very well.
不,不是真的。您可以将 yourfunc.func_code.co_code(实际编译的字节码)写入文件,然后尝试使用 decompyle 或 unpyc 对其进行反编译,但是这两个项目都是旧的且未维护的,并且从未很好地支持反编译。
It's infinitely easier to simply write your function to a file to start with.
简单地将您的函数写入文件以开始使用要容易得多。
回答by Bruno Gola
Unless there is a way of doing it on the activestate shell, no, there is no way to retrieve the exact code you've typed on the shell. At least on Linux, using the Python Shell provided by CPython there is no special way to achieve this. Maybe using iPython.
除非有一种方法可以在 activestate shell 上执行此操作,否则无法检索您在 shell 上键入的确切代码。至少在 Linux 上,使用 CPython 提供的 Python Shell 没有特殊的方法来实现这一点。也许使用 iPython。
The func_code attribute is an object representing the function bytecode, the only thing you can get from this object is bytecode itself, not the "original" code.
func_code 属性是一个表示函数字节码的对象,您可以从该对象中获得的唯一内容是字节码本身,而不是“原始”代码。