import statement within a function

Fredrik Lundh fredrik at effbot.org
Fri Feb 2 03:10:37 EST 2001


Tomasz Lisowski wrote:
> I have seen often import statements included within a function's body, not
> in the global scope, as it is most common.
> ...
> What are the advantages of the first approach over the second. Won't Python
> do the importing job many times, each time the function fun() is called?

Depends on what you mean with "importing job" -- Python
caches imported modules in sys.modules.  The "real" import
is only done the first time you need a module.

also see:
http://effbot.org/guides/import-confusion.htm
and the language reference.

Cheers /F

<!-- (the eff-bot guide to) the standard python library:
http://www.pythonware.com/people/fredrik/librarybook.htm
-->






More information about the Python-list mailing list