preserving namespaces with sub-packages

Gordon McMillan gmcm at
Sun Dec 2 09:37:50 EST 2001

Kevin Altis wrote:

>   framework.widgie.Button
[that is, Button is a class in module widgie]


> framework directory
>   various module files for the framework
>   widgie directory

[and then]
>   framework.widgie.button.Button
[but wants that referenced as framework.widgie.Button]

You have to hack widgie/ so that Button (and siblings)
are top-level attributes. Just be aware that when the programmer
has a problem with Button and goes looking for the code, it's not
going to be where he expects to find it.
> In addition, it would be nice have automatically load all
> the files in the widgie directory rather than having to hard-code each
> filename. Hopefully I'm explaining the problem correctly. Any
> suggestions or solutions?

No you don't. You don't want button automatically loaded - 
you want it to disappear!

Even without that namespace hackery, doing an "automatic load" of 
submodules is a generally a poor idea - it makes for a very 
expensive import. Perhaps the programmer only wanted one submodule,
yet he pays for all of them.

-- Gordon

More information about the Python-list mailing list