preserving namespaces with sub-packages

Gordon McMillan gmcm at hypernet.com
Sun Dec 2 15:37:50 CET 2001


Kevin Altis wrote:

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

[refactors]

> framework directory
>   __init__.py
>   various module files for the framework
>   widgie directory
>     __init__.py
>     basewidgie.py
>     button.py

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

You have to hack widgie/__init__.py 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 __init__.py 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
http://www.mcmillan-inc.com/



More information about the Python-list mailing list