indirect import of standard module

alisonken1 alisonken1 at gmail.com
Tue Apr 18 19:18:32 EDT 2006


Although 'namespace' may be a misnomer, module interfaces are 'exposed'
to the module that imports it - it's not imported a second time into
the new 'namespace'. The confusion comes about thinking that modules
and classes are related.

When a module is first imported, an instance is created for the module
and the public interfaces/variables are exposed to the module that did
the import. When another call to import the same module from somewhere
else, Python recognizes that the module has already been imported, so
it only creates a reference link to the new module that called import.
That is why you get the same ID number for the module function calls.

It's similar to classes in that Python keeps track of 'instances' of
classes as well as 'instances' of modules, the 'class' instance is
based upon variables which can be either the same class (think of a
call to a function with a class instance as the input) or a new
instance (where the same class is reused, but with different values),
whereas modules are based upon executable bytecode where there is only
one instance at all times during the program execution.




More information about the Python-list mailing list