Importing and namespace visibility

bruno modulix onurb at xiludom.gro
Mon May 16 11:36:35 EDT 2005


jean-marc wrote:
> As an application programmer, I'm not well versed in the material
> aspects of computing (memory, cpu, bus and all). My understanding of
> imports in Python is such: the __main__ program is the center piece
> which holds the programs reference: globals, functions, classes,
> modules etc. 

the '__main__' program is in fact just another Python module. What makes 
it the 'main program' is the fact that you pass it to the Python 
interpreter as a 'main program'.

> The objects of this file (functions and classes) are
> directly accessible; 

from within this particular module only.

> 'import suchModule' s objects are attainable
> through the *qualified name* (module.function); the 'from suchModule
> import *' the objects are directly attainable.
 >
Take care, this has other implications too.

(snip)
 >
> BUT, of all this I thought that if you import module1, then module2
> (into __main__), objects from module1 would be available to objects of
> module2 which came (into memory space) after module1 was loaded. This
> does not seem to be the case, 

This is not.

> and module2 requires an 'import module1'

Right. And that's a Good Thing(tm).

> statement in its own file to see this last module's objects.  This is
> not the recursive situation that was a pitfall Fredrik was evoking.
> What am I missing here???

The term 'global' in Python is somewhat misleading. What it really means 
is "global to the current module". Each module (and remember the 'main 
program' is a module too) has it's own namespace.

-- 
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for 
p in 'onurb at xiludom.gro'.split('@')])"



More information about the Python-list mailing list