Importing again and again

John Salerno johnjsal at NOSPAMgmail.com
Thu Jun 8 22:48:39 CEST 2006


abcd wrote:

> def foo():
>     import bar
>     bar.printStuff()
> 
> foo()
> foo()
> foo()
> foo()
> 
> ...will that re-import bar 4 times...or just import it once?  is this a
> big performance hit?
> 
> thanks
> 

Given a file called bar.py with the following contents:

print "I'm being imported!"

def printStuff():
     print 'stuff'

I get this output when I import foo.py:

 >>> import foo
I'm being imported!
stuff
stuff
stuff
stuff
 >>>





More information about the Python-list mailing list