简体   繁体   English

在交互式 shell 中显示函数定义

[英]Display function definition in interactive shell

I am working in the Python Interactive Shell (ActiveState ActivePython 2.6.4 under Windows XP).我在 Python 交互式 Shell(Windows XP 下的 ActiveState ActivePython 2.6.4)中工作。 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. dir() 显示了“ code ”和“func_code”属性,但我不知道它们是否包含我需要的内容。

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 except Windows), you might exploit the fact that readline itself does remember 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 等平台上。

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 .安装dill运行pip install dill To get it formatted:要格式化:

>>> 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

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.将您的函数简单地写入文件开始变得非常容易。

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.除非有办法在 activestate shell 上执行此操作,否则无法检索您在 shell 上键入的确切代码。 At least on Linux, using the Python Shell provided by CPython there is no special way to achieve this.至少在 Linux 上,使用 CPython 提供的 Python Shell 没有特殊的方法可以实现这一点。 Maybe using iPython.也许使用 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 属性是一个代表函数字节码的对象,你可以从这个对象中得到的唯一东西是字节码本身,而不是“原始”代码。

You can try pressing "ctrl+r" (ie reverse i search in bash) and then typing您可以尝试按“ctrl+r”(即我在 bash 中反向搜索)然后输入

def <name of your function> 

and then pressing enter , it might reveal the function definition然后按 enter ,它可能会显示函数定义

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM