import and package confusion

Dave Angel davea at
Thu Apr 30 04:02:46 CEST 2009

Dale Amon wrote:
> The point I take away from this is that packages and
> modules have dotted names, but Classes do not and there
> is no way to do exactly what I wanted to do. 
> The dot syntax would have been quite nice (I quite like the
> "::" syntax in Perl) and would have made the code much
> clearer. The way you suggested with a 'typename_classname'
> generated using a from/import statement will just have to
> suffice.
You clearly still don't understand.  The dot syntax works very 
predictably, and quite flexibly.  The problem was that by using the same 
name for module and class, you didn't realize you needed to include both.

If a particular module is called   mymodule, and you want to use a class 
within it called  myclass, you can do it readily with:

import mymodule
obj = mymodule.myclass(arg1, arg2)

If you have the same class name in more than one module, you can make 
either one simply by doing:

obj =  mymodule1.myclass(arg1, arg2)
obj = .mymlodule2.myclass(arg1, arg2)

And in particular if you simply do the following, you can choose between 
those modules:

if  test:
    mod = mymodule1
   mod = mymodule2
obj = mod.myclass(arg1, arg2)

Now the same logic would work if you added packages to the mix, and if 
you happened to call the class the same thing as the module.  But it 
would be very confusing in a message like this, so I choose not to.

The compiler doesn't care if the names are the same, but when you get 
scoping wrong, the error messages will be confusing.

Please don't sink to exec or eval to solve what is really a 
straightforward problem.

More information about the Python-list mailing list