iPython locals() 中的 '_oh' 是什么?以及如何让当地人回来或在标准 python IDLE 中创建新的当地人

What is '_oh' in iPython locals()? And how to get locals back or create new one in standard 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:

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 上尝试了标准 python 提示(Ubuntu).不要认为这有什么不同,为了完整起见添加它。

在 运行 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() 有什么价值。它可能 'safer' 在一个函数中。

Magic %who 显示交互变量,即您自己的代码创建的变量。可能有一种方法可以选择性地删除它们,但我不需要它。如果我需要一个干净的 ipython 会话,我只需在新的 shell 选项卡或 window.

中启动一个

我已经很久没有使用 IDLE(甚至都没有安装它),所以无法帮助它 locals()。它有一个更简单的历史机制。

看看魔术,有一个%reset可以用来删除用户定义的变量,甚至清除In/Out历史。阅读它的文档。

另见 %who_ls%reset_selective