我在 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 提示符。不要认为这有什么不同,为了完整起见添加它。