Le Jeudi 08 Juin 2006 22:02, abcd a écrit :
>
> 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?
Import a module more than once doesn't execute the code of this mo
abcd wrote:
> If I have code which imports a module over and over again...say each
> time a function is called, does that cause Python to actually re-import
> it...or will it skip it once the module has been imported??
>
> for example:
>
> def foo():
> import bar
> bar.printStuff()
>
> f
it's import-ed only once
# magic.py file
#!/usr/bin/python
print "here"
import magic# try to import itself
then try
# bad_magic.py
#!/usr/bin/python
print "here"
import bad_magic
reload(bad_magic)
hth, Daniel
--
http://mail.python.org/mailman/listinfo/python-list
"Terry Reedy" <[EMAIL PROTECTED]> wrote:
>
> "John Bokma" <[EMAIL PROTECTED]> wrote in message
> news:[EMAIL PROTECTED]
>>> def foo():
>>> import bar
>>> bar.printStuff()
>> I am new to Python so this might be a weird question, but it there a
>> reason why you import bar inside foo?
>
>
"John Bokma" <[EMAIL PROTECTED]> wrote in message
news:[EMAIL PROTECTED]
>> def foo():
>> import bar
>> bar.printStuff()
> I am new to Python so this might be a weird question, but it there a
> reason why you import bar inside foo?
Two possible reasons:
1) delay import until actually nee
"abcd" <[EMAIL PROTECTED]> wrote:
> If I have code which imports a module over and over again...say each
> time a function is called, does that cause Python to actually re-import
> it...or will it skip it once the module has been imported??
>
> for example:
>
> def foo():
> import bar
>
abcd írta:
> If I have code which imports a module over and over again...say each
> time a function is called, does that cause Python to actually re-import
> it...or will it skip it once the module has been imported??
>
> for example:
>
> def foo():
> import bar
> bar.printStuff()
>
> foo()
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
abcd wrote:
> If I have code which imports a module over and over again...say each
> time a function is called, does that cause Python to actually re-import
> it...or will it skip it once the module has been imported??
>
> for example:
>
> def foo():
> import bar
> bar.printStuff()
>
> foo