相关疑难解决方法(0)

importlib.reload 是否应该在 Python 3.6 中恢复已删除的属性?

我正在研究这两个相关的问题:herehere

我在 Python 3.6 中看到了我不期望的行为,这与reload在 Python 2.7(和 3.4)中使用普通的行为不同。也就是说,似乎在模块初始化期间或在重新加载期间重新执行模块时填充的模块属性在其本地名称被删除后不会恢复del......见下文:

对于 Python 3.6:

In [1]: import importlib

In [2]: import math

In [3]: del math.cos

In [4]: math.cos
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-4-05b06e378197> in <module>()
----> 1 math.cos

AttributeError: module 'math' has no attribute 'cos'

In [5]: math = importlib.reload(math)

In [6]: math.cos
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-6-05b06e378197> in <module>()
----> 1 math.cos

AttributeError: module 'math' has no …
Run Code Online (Sandbox Code Playgroud)

python cpython python-3.x python-internals

5
推荐指数
1
解决办法
611
查看次数

标签 统计

cpython ×1

python ×1

python-3.x ×1

python-internals ×1