首页 > 解决方案 > 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__不起作用。

如果问题 3 在此答案的范围内过于宽泛,我可以提出一个单独的问题。谢谢你。

注意:我在 Windows 10 上尝试了 IPython,在 linux (Ubuntu) 上尝试了标准 python 提示符。不要认为这有什么不同,为了完整起见添加它。

标签: pythonpython-3.xnamespacesipythonlocal-variables

解决方案


在运行ipython会话中:

In [109]: id(Out)
Out[109]: 139634741914432
In [110]: id(_oh)
Out[110]: 139634741914432

_ohOut字典的另一个名称,我们可以将其用作:

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


推荐阅读