gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 16:54:05 54 4
gpt4 key购买 nike

我在 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__不起作用。
  • 问题一:如果赋值 ( __builtins__ = builtins ) 有效,为什么我无法访问内置函数?
  • 问题2:什么是_ohiPython作为一个空荡荡的人,它如何帮助本地人回归 dict本身。
  • 问题三:如何获得locals()locals().clear() 之后在 python IDLE 中工作(不重启IDLE)

  • 如果问题 3 在本答案的范围内过于宽泛,我可以提出一个单独的问题。谢谢你。
    注意:我在 Windows 10 上尝试了 IPython,在 linux (Ubuntu) 上尝试了标准的 python 提示。不要认为这有什么不同,为了完整起见添加了它。

    最佳答案

    在运行中 ipython session :

    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() 有什么值(value).它可能在函数内部“更安全”。
    魔术 %who显示交互式变量,即您自己的代码创建的变量。可能有一种方法可以选择性地删除那些,但我不需要它。如果我需要清洁 ipython session 我只是在一个新的 shell 选项卡或窗口中启动一个。
    我很久没有使用 IDLE(甚至没有安装它),所以无法帮助它 locals() .它有一个更简单的历史机制。
    看看魔法,有一个 %reset可以用来删除用户定义的变量,甚至清除 In/Out历史。阅读它的文档。
    另见 %who_ls%reset_selective .

    关于python - iPython locals() 中的 '_oh' 是什么?以及如何在标准 Python IDLE 中让本地人回来或创建新人,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65774484/

    54 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com