鉴于这种情况:
你知道吗Foo.py公司地址:
import another_module
def foo():
#some code
它是由不同的模块调用的,其中一些模块已经import another_module
,而另一些模块则没有主.py')
- Which is the default behaviour? Is the module re-loaded?If yes,(that's just a curiosity) let's suppose that another_module has changed between the import in the main and the import in
foo.py
. Python does have in memory two different version ofanother_module
,one accessible forfoo.py
and one formain.py
?- Is there a way to not
import another_module
if it has already been done in themain
?- How does it works if in the main i have
from another_module import f1
, and in foo.pyfrom another_module import f1,f2
.Is f2 just "added" or module reloaded?
不,模块没有重新加载。可以使用^{} 函数重新加载模块。导入模块后,它将被添加到^{} 字典中。如文档所述,还可以通过更改此变量的内容来强制重新加载模块:
^{} 语句对于这种行为没有任何区别。它只控制加载模块的哪些部分被添加到当前作用域:
您可以自己轻松验证:
年产量
x.py:
测试.py:
相关问题 更多 >
编程相关推荐