Re: Importing again and again
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 module more than once. I don't know what's your need but as some have spoke of reload I just want to warn you, reload a module means that you want invalidate the code of this and replace it by a new one, this is not like a normal but deeper import. Also, you'll have to deal yourself wiith references to your old code. Hmmm, the following example should be clear than my explanations :) n [1]: import temp In [2]: class a(temp.Base temp.Base In [2]: class a(temp.Base temp.Base In [2]: class a(temp.Base temp.Base In [2]: class a(temp.Base) : pass ...: In [3]: reload(temp) Out[3]: In [4]: class b(temp.Base) : pass ...: In [7]: b.__base__, a.__base__, b.__base__ is a.__base__ Out[7]: (, , False) In [8]: isinstance(a(), temp.Base), isinstance(b(), temp.Base) Out[8]: (False, True) -- _ Maric Michaud _ Aristote - www.aristote.info 3 place des tapis 69004 Lyon Tel: +33 426 880 097 -- http://mail.python.org/mailman/listinfo/python-list
Re: Importing again and again
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() > foo() > foo() > foo() > > ...will that re-import bar 4 times...or just import it once? is this a > big performance hit? > > thanks > To address the performance question, by the way, the first thing that the import process does is look in sys.modules to see whether the module has already been imported. If so then it uses the module referenced by sys.modules, so the overhead is pretty small (dicts being quite fast generally speaking). So your code is a reasonable way to defer the import of bar until the function is called, which is why I presumed you coded it that way. regards Steve -- Steve Holden +44 150 684 7255 +1 800 494 3119 Holden Web LLC/Ltd http://www.holdenweb.com Love me, love my blog http://holdenweb.blogspot.com Recent Ramblings http://del.icio.us/steve.holden -- http://mail.python.org/mailman/listinfo/python-list
Re: Importing again and again
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
Re: Importing again and again
"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? > > Two possible reasons: > 1) delay import until actually needed, if ever. > 2) put 'bar' into the function local namespace instead of the module > global namespace OK clear and thanks. I was guessing the later, and the first one I overlooked (Perl works different, you have to do the delay yourself). -- John MexIT: http://johnbokma.com/mexit/ personal page: http://johnbokma.com/ Experienced programmer available: http://castleamber.com/ Happy Customers: http://castleamber.com/testimonials.html -- http://mail.python.org/mailman/listinfo/python-list
Re: Importing again and again
"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 needed, if ever. 2) put 'bar' into the function local namespace instead of the module global namespace Terry Jan Reedy -- http://mail.python.org/mailman/listinfo/python-list
Re: Importing again and again
"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 > bar.printStuff() > > foo() > foo() > foo() > foo() > > ...will that re-import bar 4 times...or just import it once? is this a > big performance hit? I am new to Python so this might be a weird question, but it there a reason why you import bar inside foo? -- John MexIT: http://johnbokma.com/mexit/ personal page: http://johnbokma.com/ Experienced programmer available: http://castleamber.com/ Happy Customers: http://castleamber.com/testimonials.html -- http://mail.python.org/mailman/listinfo/python-list
Re: Importing again and again
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() > foo() > foo() > foo() > > ...will that re-import bar 4 times...or just import it once? Just once. Try this: bar.py: print "I have been imported" def printStuff(): print "printStuff was called" foo.py: def foo(): import bar bar.printStuff() foo() foo() foo() The result is: I have been imported printStuff was called printStuff was called printStuff was called If you really need to reimport the module, you can do this: foo.py: import bar def foo(): global bar bar = reload(bar) bar.printStuff() foo() foo() foo() The result is: I have been imported I have been imported printStuff was called I have been imported printStuff was called I have been imported printStuff was called > Is this a big performance hit? > It depends on the size of your 'bar.py' module, and also it depends on how often you need to change/reload while your program is running. Best, Laszlo -- http://mail.python.org/mailman/listinfo/python-list
Re: Importing again and again
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 >>> -- http://mail.python.org/mailman/listinfo/python-list
Re: Importing again and again
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() > foo() > foo() > foo() > > ...will that re-import bar 4 times...or just import it once? is this a > big performance hit? > > thanks I don't really know, but I do know that the right way to re-import a module is: reload(bar) -- http://mail.python.org/mailman/listinfo/python-list
Importing again and again
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() foo() foo() foo() ...will that re-import bar 4 times...or just import it once? is this a big performance hit? thanks -- http://mail.python.org/mailman/listinfo/python-list