正在导入相同模块的不同文件 [英] Same module is being imported in different files
问题描述
执行此操作是不好的做法:
Is it a bad practice to do this:
在first.py中:
In first.py:
import second
import mymodule
在second.py:
In second.py:
import mymodule
正在两个文件中导入
mymodule
,并且first.py导入second.py。是否有可能以某种方式导入 mymodule
一次?这不是什么大不了的事,它只是不优雅,也不是Pythonic IMO。
mymodule
is being imported in both files and first.py imports second.py. Is it possible to somehow import the mymodule
just once? It's not a big deal, it's just not elegant nor Pythonic IMO.
推荐答案
mymodule
只运行一次;每个导入它的模块都共享同一个副本。没关系。如果您有任何循环导入(A导入B导入A,或导入B导入C ...导入A),请小心,因为这些可能导致细微的初始化顺序问题并使您搞砸。
mymodule
is only run once; every module that imports it shares the same copy. It's fine. Just be careful if you have any cyclic imports (A imports B imports A, or A imports B imports C ... imports A), because those can cause subtle initialization order problems and mess you up.
这篇关于正在导入相同模块的不同文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!