Importing * From a Package

greg greg at cosc.canterbury.ac.nz
Wed Aug 8 04:06:39 CEST 2007


Patrick Doyle wrote:
> This is the part that has me confused -- why does "from package import
> *" go on to import names that were explicitly loaded by previous
> import statements?

Because there's no easy way for it *not* to do that.
All it does is grab whatever names are defined in the
module at the time. It can't tell whether those names
got there as a result of an import statement or some
other way.

A more pertinent question is why it *doesn't* import
submodules that haven't already been explicitly imported.
Probably this is just because it could be quite expensive
to do so -- you would potentially be triggering a lot
of disk activity to load the previously unused modules
into memory, many of which you may not be going to use.

Just in case it's not clear, "importing" can actually
mean two different things. If the module hasn't been
imported before, it has to be loaded into memory, which
is expensive. If it has been imported before, then
you're just creating another reference to the same
module object, which is very cheap. An import *
just does the cheap part, and leaves you to explicitly
ask for the expensive part if you really want it.

--
Greg



More information about the Python-list mailing list