importing question ?

Robin Munn rmunn at pobox.com
Fri Nov 29 13:44:24 EST 2002


Martyn Quick <mrq at for.mat.bham.ac.uk> wrote:
> On Thu, 28 Nov 2002, Jonas Geiregat wrote:
> 
>>  >>> import classes
>>  >>> a = veryBigSnake()
>> Traceback (most recent call last):
>>    File "<stdin>", line 1, in ?
>> NameError: name 'veryBigSnake' is not defined
>>  >>> from classes import *
>>  >>> a = veryBigSnake()
>> 
>> why doesn't the first import work ?
>> and the second does
>> 
>> and what is the difference between those two ?
> 
> With the first you are importing the command in the form
> classes.veryBigSnake, so the following would work:
> 
> import classes
> a = classes.veryBigSnake()
> 
> and would give the same as
> 
> from classes import *
> a = veryBigSnake()
> 
> The difference is that the second hides where veryBigSnake comes from and
> can cause confusion if you were to change the meaning of the function in
> the middle of your code.  With the first you're less likely to do this.
> 
> Experts can explain this in the proper technical language better than I
> can.  ;-)

The "proper technical language" for this sort of thing is "namespaces".
Python names (function names, variable names, class names, whatever)
live in namespaces. Namespaces themselves also have names -- or, rather,
they're attached to named objects. Let me explain. Say you create a
class "myClass", like so:

    class myClass:
        foo = 1
        bar = "Testing'

The name "myClass" refers to a class object. That class object has a
namespace attached, and that namespace contains two names, foo and bar.
You can access these names as "myClass.foo" and "myClass.bar".

Modules also have namespaces. If the above class definiton was in a
module called "mymodule.py", this would be the correct way to access
"foo" and "bar" from the myClass definition:

    import mymodule
    print mymodule.myClass.foo
    print mymodule.myClass.bar

Note that myClass is inside the mymodule namespace, so it is accessed
via the dot operator (which looks up names in namespaces). If you wanted
myClass to get imported into the global namespace, the way to do it is:

    from mymodule import myClass
    print myClass.foo
    print myClass.bar

"from <module> import <name>" finds the name in the namespace of that
module, and imports that name into the namespace that you are currently
running in (usually this will be the global namespace). It is *always* a
bad idea to do "from <module> import *"! A few modules are designed to
be used this way, but they specifically say so in their documentation
and usually have naming conventions designed to prevent namespace
collision.

One last concept: the global namespace. The global namespace is the
highest-level namespace. When you run a Python program, it first starts
executing in the global namespace, and all assignment statements, import
statements, etc. place names into the global namespace. Other namespaces
can be accessed through names, like "myClass.foo" which accesses the
myClass namespace via the name myClass -- a name that resides in the
global namespace. But the global namespace does not have a name attached
to it (which namespace would that name reside in, anyway?). Sometimes
you might need to get access to the global namespace, and that is what
the globals() function is for -- it returns the global namespace (as a
Python dictionary, IIRC). You can then use that object for whatever
purposes you needed (like executing a particular bit of code in the
global namespace instead of a local namespace, for example).

I hope this helps someone understand namespaces better.

-- 
Robin Munn <rmunn at pobox.com>
http://www.rmunn.com/
PGP key ID: 0x6AFB6838    50FF 2478 CFFB 081A 8338  54F7 845D ACFD 6AFB 6838



More information about the Python-list mailing list