简体   繁体   English

有没有办法看到你在Python CMD中输入的函数代码

[英]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.getsourcelinesinspect模块,但它只打印源文件中定义的函数的代码。

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

相关问题 有什么方法可以查看Python 3模块中的源函数吗? - Is there any way to see source functions in Python 3 Modules? 你知道有什么方法可以缩短 Python 中的 4 个函数吗? - Do you know any way to shorten 4 functions in Python? 有没有办法看到你在 ursina 的场景中投放的光线投射? - is there a way to see the raycast you put out into your scene in ursina? 有没有办法看到python包的所有版本? - Is there any way to see all the versions for a python package? 有没有办法查看字符串是否包含Python中的任何符号? - Is there a way to see if a string contains any symbols in Python? 你在Python方法和函数中看到了很多CamelCase吗? - do you see CamelCase much in Python methods and functions? 是否有任何IDE中的调试功能可以让您查看代码所采用的“路径”? - Is there a debugging feature in any IDE that lets you see what “path” your code took? 我的cmd有没有办法从Python中打印出任何unicode? - Is there a way for my cmd to print out any unicode from within Python? 有没有办法通过windows cmd运行python脚本并在cmd中显示python操作和绘图 - is there any way to run python scripts through windows cmd and show python operation and plots in cmd 没有管理员权限时如何在 Windows 中安装 Python(任何版本)? - How to install Python (any version) in Windows when you've no admin privileges?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM