这是正确的吗?多次导入相同的模块。 [英] Is this right? Multiple imports of same module.

查看:71
本文介绍了这是正确的吗?多次导入相同的模块。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从我收集到的一些消息中,这是多次导入

相同模块的工作方式。说,我有3个模块(mod1.py,mod2.py和

mod3.py),在同一个会话中,他们都导入另一个模块

mymodule.py like这个:


*在mod1.py


导入mymodule


*在mod2.py


导入mymodule


*在mod3.py


导入mymodule为MM


然后mymodule只导入一次,但每个模块都可以通过模块名称(mod1和mod2)和别名MM(mod3)访问它。


对吗?


我担心多次进口和效率。


感谢任何信息。


- Lou Pecora(我的观点是我自己的)删除这封给我发电子邮件。

From what I gleaned on some messages this is the way multiple imports of
the same module work. Say, I have 3 modules (mod1.py, mod2.py, and
mod3.py) and in the same session they all import another module
mymodule.py like this:

*In mod1.py

import mymodule

*In mod2.py

import mymodule

*In mod3.py

import mymodule as MM

Then mymodule is imported only once, but each module has access to it
through the module name (mod1 and mod2) and the alias MM (mod3). Is
that right?

I was concerned about multiple imports and efficiency.

Thanks for any info.

-- Lou Pecora (my views are my own) REMOVE THIS to email me.

推荐答案

在< pe ************************** @ ra.nrl.navy.mil>中,Lou Pecora写道:
In <pe**************************@ra.nrl.navy.mil>, Lou Pecora wrote:

*在mod1.py


导入mymodule


*在mod2.py


导入mymodule


*在mod3.py


导入mymodule为MM

然后mymodule只导入一次,但每个模块都可以通过模块名称(mod1和mod2)和别名MM(mod3)访问它。


对吗?
*In mod1.py

import mymodule

*In mod2.py

import mymodule

*In mod3.py

import mymodule as MM

Then mymodule is imported only once, but each module has access to it
through the module name (mod1 and mod2) and the alias MM (mod3). Is
that right?



是的,这是正确的。


Ciao,

Marc''BlackJack ''Rintsch

Yes, that''s correct.

Ciao,
Marc ''BlackJack'' Rintsch


2007年3月9日,Lou Pecora< pe **** @ anvil.nrl.navy.milwrote:
On 3/9/07, Lou Pecora <pe****@anvil.nrl.navy.milwrote:

>

*在mod1.py


导入mymodule


*在mod2.py


导入mymodule


*在mod3.py


导入mymodule作为MM


然后mymodule只导入一次,但是每个模块都可以通过模块名称(mod1和mod2)和别名MM访问
量mod3)。

对吗?
>
*In mod1.py

import mymodule

*In mod2.py

import mymodule

*In mod3.py

import mymodule as MM

Then mymodule is imported only once, but each module has access to it
through the module name (mod1 and mod2) and the alias MM (mod3). Is
that right?



是的,有时做这样的事情很有用......

Yup, and occasionally it''s useful to do stuff like this...


I关注多种进口和效率。
I was concerned about multiple imports and efficiency.



....但不是因为这个原因。随后导入已经加载的

模块非常快。


-

干杯,

Simon B
si***@brunningonline.net
http://www.brunningonline.net/simon/blog/

3月9日下午3:30,Lou Pecora< pec ... @ anvil.nrl.navy.milwrote:
On Mar 9, 3:30 pm, Lou Pecora <pec...@anvil.nrl.navy.milwrote:

然后导入mymodule只有一次,但每个模块都可以通过模块名称(mod1和mod2)和别名MM(mod3)访问它。


对吗?
Then mymodule is imported only once, but each module has access to it
through the module name (mod1 and mod2) and the alias MM (mod3). Is
that right?



是的,它是。

Yes, it is.


我担心多次进口和效率。
I was concerned about multiple imports and efficiency.



如果模块已经加载,则不会再次加载。字典

加载的模块可以作为sys.modules使用。

If the module is already loaded, it won''t be loaded again. Dictionary
of loaded modules is avaliable as sys.modules.


这篇关于这是正确的吗?多次导入相同的模块。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆