[英]Is there any way to see the codes of functions you've typed in your Python CMD
Just found out that when I pressed up in my cmd one of my function codes is no longer accessible meaning I couldn't see the code I made it with. 刚刚发现当我在我的cmd中按下一个我的功能代码时,我的功能代码不再可访问,这意味着我看不到我用它编写的代码。 Is there any way I can see it again?
有什么办法可以再看一遍吗? And is there any way I could see all the functions I created for this session in Python cmd?
有什么方法可以在Python cmd中看到我为此会话创建的所有函数?
dir() should give you everything. dir()应该给你一切。
EDIT 编辑
>>> def blah():
... return 1
...
>>> dir()
['__builtins__', '__doc__', '__name__', 'blah']
>>>
>>> dir(blah.func_code)
['__class__', '__cmp__', '__delattr__', '__doc__', '__getattribute__', '__hash__
', '__init__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr_
_', '__str__', 'co_argcount', 'co_cellvars', 'co_code', 'co_consts', 'co_filenam
e', 'co_firstlineno', 'co_flags', 'co_freevars', 'co_lnotab', 'co_name', 'co_nam
es', 'co_nlocals', 'co_stacksize', 'co_varnames']
>>> blah.func_code.co_code
'd\x01\x00S'
You can "delete" a function by assigning None
to it. 您可以通过为其指定“
None
”来“删除”该功能。
EDIT 编辑
If you want to know what you typed, you should probably put your code inside a file and import or execute it. 如果您想知道键入的内容,您应该将代码放在文件中并导入或执行它。 The buffer of the Python interactive interpreter is limited.
Python交互式解释器的缓冲区是有限的。
I think your best bet is to do something like: 我认为你最好的选择是做一些事情:
import readline
readline.write_history_file("myhistory")
in the python shell, where you defined the function. 在python shell中,您定义了该函数。
You can find your function in the "myhistory"-file afterwards. 您可以在“myhistory”文件中找到您的函数。
There exists an inspect
module with inspect.getsourcelines
, but that only prints code for functions defined in source-files. 存在一个带有
inspect.getsourcelines
的inspect
模块,但它只打印源文件中定义的函数的代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.