我在 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)