Is this right? Multiple imports of same module.

Lou Pecora pecora at anvil.nrl.navy.mil
Fri Mar 9 17:18:47 CET 2007


In article <pan.2007.03.09.15.33.01.991831 at gmx.net>,
 Marc 'BlackJack' Rintsch <bj_666 at gmx.net> wrote:

> In <pecora-B92BB8.09304609032007 at ra.nrl.navy.mil>, Lou Pecora wrote:
> 
> > *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?
> 
> Yes, that's correct.
> 
> Ciao,
> 	Marc 'BlackJack' Rintsch

Thank you.

A further confusion (if I may):

I have noticed that using from xxx import * can lead to problems when 
trying to access variables in the xxx module.

E.g.

-- File f2.py

imvar=1

def prn():
   print imvar

-- File junk.py

from f2 import *
prn()
imvar=2
prn()

-- Running junk.py gives the output:

1
1

Not what I would expect which is,

1
2

Namespaces get confusing.  I have begun to switch to using 'import f2 
as' which gives the 2nd expected result. Safer, but still a bit puzzling.

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



More information about the Python-list mailing list