python - iPython locals() 中的“_oh”是什么?以及如何让本地人回来或在标准 python IDLE 中创建新的
问题描述
我在 IPython (Spyder) 中使用命名空间,并试图看看如果我dict.clear()
locals()
. 所以,事不宜迟:
Python 3.8.5 (default, Aug 5 2020, 09:44:06) [MSC v.1916 64 bit (AMD64)]
Type "copyright", "credits" or "license" for more information.
IPython 7.19.0 -- An enhanced Interactive Python.
In [1]: locals().clear()
In [2]: locals
Traceback (most recent call last):
File "<ipython-input-2-f1c14746c80d>", line 1, in <module>
locals
File "C:\Users\sayan\Anaconda3\envs\tfgpu_py38\lib\site-packages\IPython\core\displayhook.py", line 263, in __call__
self.update_user_ns(result)
File "C:\Users\sayan\Anaconda3\envs\tfgpu_py38\lib\site-packages\IPython\core\displayhook.py", line 201, in update_user_ns
if self.cache_size and result is not self.shell.user_ns['_oh']:
KeyError: '_oh'
In [3]: dict
Traceback (most recent call last):
File "<ipython-input-3-0d8c7dca5f1a>", line 1, in <module>
dict
File "C:\Users\sayan\Anaconda3\envs\tfgpu_py38\lib\site-packages\IPython\core\displayhook.py", line 263, in __call__
self.update_user_ns(result)
File "C:\Users\sayan\Anaconda3\envs\tfgpu_py38\lib\site-packages\IPython\core\displayhook.py", line 201, in update_user_ns
if self.cache_size and result is not self.shell.user_ns['_oh']:
KeyError: '_oh'
In [4]: globals
Traceback (most recent call last):
File "<ipython-input-6-0d1754e6861d>", line 1, in <module>
globals
File "C:\Users\sayan\Anaconda3\envs\tfgpu_py38\lib\site-packages\IPython\core\displayhook.py", line 263, in __call__
self.update_user_ns(result)
File "C:\Users\sayan\Anaconda3\envs\tfgpu_py38\lib\site-packages\IPython\core\displayhook.py", line 201, in update_user_ns
if self.cache_size and result is not self.shell.user_ns['_oh']:
KeyError: '_oh'
由于内置函数具有单独的命名空间,因此尝试删除local
:
In [5]: del locals
Traceback (most recent call last):
File "<ipython-input-5-61828b7e8872>", line 1, in <module>
del locals
NameError: name 'locals' is not defined
预期,因为命名空间本身已被删除。
我尝试导入builtins
并分配给__builtins__
In [6]: import builtins
In [7]: __builtins__ = builtins
In [8]: __builtins__.dict
Traceback (most recent call last):
File "<ipython-input-11-8211c7f1d719>", line 1, in <module>
__builtins__.dict
File "C:\Users\sayan\Anaconda3\envs\tfgpu_py38\lib\site-packages\IPython\core\displayhook.py", line 263, in __call__
self.update_user_ns(result)
File "C:\Users\sayan\Anaconda3\envs\tfgpu_py38\lib\site-packages\IPython\core\displayhook.py", line 201, in update_user_ns
if self.cache_size and result is not self.shell.user_ns['_oh']:
KeyError: '_oh'
在这一点上,我注意到所有错误都归因于_oh
. 所以我重新启动了内核,并检查了本地:
locals()
Out[1]:
{'__name__': '__main__',
'__doc__': 'Automatically created module for IPython interactive environment',
'__package__': None,
'__loader__': None,
'__spec__': None,
'__builtin__': <module 'builtins' (built-in)>,
'__builtins__': <module 'builtins' (built-in)>,
'_ih': ['', 'locals()'],
'_oh': {},
'_dh': ['D:\\Programs\\Python\\StackOv'],
'In': ['', 'locals()'],
'Out': {},
'get_ipython': <bound method InteractiveShell.get_ipython of <ipykernel.zmqshell.ZMQInteractiveShell object at 0x000002C2953F37F0>>,
'exit': <IPython.core.autocall.ZMQExitAutocall at 0x2c295420af0>,
'quit': <IPython.core.autocall.ZMQExitAutocall at 0x2c295420af0>,
'_': '',
'__': '',
'___': '',
'_i': '',
'_ii': '',
'_iii': '',
'_i1': 'locals()'}
所以_oh
似乎是一本空字典。现在:
In [2]: locals().clear()
In [3]: dict
Traceback (most recent call last):
File "<ipython-input-3-0d8c7dca5f1a>", line 1, in <module>
dict
File "C:\Users\sayan\Anaconda3\envs\tfgpu_py38\lib\site-packages\IPython\core\displayhook.py", line 263, in __call__
self.update_user_ns(result)
File "C:\Users\sayan\Anaconda3\envs\tfgpu_py38\lib\site-packages\IPython\core\displayhook.py", line 201, in update_user_ns
if self.cache_size and result is not self.shell.user_ns['_oh']:
KeyError: '_oh'
In [4]: locals()['_oh'] = {}
In [5]: dict
Out[5]: dict
所以我找到了如何locals
返回,但不明白为什么分配一个空dict
作为_oh
带回来locals
。
但同样的事情在 python IDLE 中不起作用:
Python 3.7.7 (default, May 7 2020, 21:25:33)
[GCC 7.3.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> locals()
{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <class '_frozen_importlib.BuiltinImporter'>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>}
>>> locals().clear()
>>> locals()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'locals' is not defined
>>> dict
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'dict' is not defined
>>> import builtins
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: __import__ not found
>>> __builtin__
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name '__builtin__' is not defined
>>>
没有_oh
,甚至__import__
不起作用。
- 问题 1:如果赋值 (
__builtins__ = builtins
) 有效,为什么我无法访问内置函数? - 问题 2:里面有什么
_oh
以及iPython
它如何帮助把当地人带回来,因为它dict
本身就是一个空虚。 - 问题3:如何
locals()
在python IDLE中工作locals().clear()
(无需重启IDLE)
如果问题 3 在此答案的范围内过于宽泛,我可以提出一个单独的问题。谢谢你。
注意:我在 Windows 10 上尝试了 IPython,在 linux (Ubuntu) 上尝试了标准 python 提示符。不要认为这有什么不同,为了完整起见添加它。
解决方案
在运行ipython
会话中:
In [109]: id(Out)
Out[109]: 139634741914432
In [110]: id(_oh)
Out[110]: 139634741914432
_oh
是Out
字典的另一个名称,我们可以将其用作:
In [112]: Out[109]
Out[112]: 139634741914432
In [113]: _oh[109]
Out[113]: 139634741914432
In [114]: _109
Out[114]: 139634741914432
所以他们为什么使用另一个名字,我不知道。无论如何,locals().clear()
您清除了您在 中看到的整个变量字典Out[1]
,并有效地禁用了ipython
交互式会话。
我不知道使用locals.clear()
. 它可能在函数内部“更安全”。
Magic%who
显示交互式变量,即您自己的代码创建的变量。可能有一种方法可以选择性地删除它们,但我不需要它。如果我需要一个干净的ipython
会话,我只需在新的 shell 选项卡或窗口中启动一个。
我很长时间没有使用 IDLE(甚至没有安装它),所以对它的locals()
. 它有一个更简单的历史机制。
看看魔术,有一个%reset
可以用来删除用户定义的变量,甚至清除In/Out
历史记录。阅读它的文档。
另见%who_ls
和%reset_selective
。
推荐阅读
- python - Numba:将 numpy 数组转换为可散列对象
- c++ - 将 std::list 附加到 std::list 的向量中
- lambda - 如何使用部署到 Netlify 的无服务器 lambda 函数隐藏条带密钥
- slurm - 如何在 SLURM 的分区中按节点组分配作业
- javascript - 使用 deleteRow 按 id 删除一行
- java - 使用 JNI 调用 C++ 库函数以及哪个进程执行该 C++ 库
- swift - 如何通过 ios 中的 cocoapods 使用 vendored 框架发送 dsym 文件
- c# - 如何添加引用 EF 核心中同一个表的多个外键
- typescript - 在运行时从数组值的 Observable 派生联合类型
- elixir - 无法运行 ecto.create,无法创建数据库:连接不可用并且请求已从队列中删除