Help with modules/packages.

Steve Holden steve at holdenweb.com
Sat Dec 4 10:53:03 EST 2004


Christopher J. Bottaro wrote:

> Hello,
>         I want to be able to say stuff like "import CJB.ClassA" and "import
> CJB.ClassB" then say "c = CJB.ClassA()" or "c = CJB.ClassB()".  CJB will be
> a directory containing files "ClassA.py" and "ClassB.py".
> 
>         Now that I think about it, that can't work because Python allows you import
> different things from the same module (file).  If I said "import
> CJB.ClassA", I'd have to instantiate ClassA like "c = CJB.ClassA.ClassA()".
> 
> I guess I could say "from CJB.ClassA import ClassA", but then I'd
> instantiate like "c = ClassA()".  What I really want is to say "c =
> CJB.ClassA()"...is that possible?
> 
> Is my understand of modules/packages correct or am I way off?
> 
> Thanks for the help.
> 
Your understanding appears to be roughly correct, but you are 
overlooking the possibility that the package could manage its own namespace.

One thing you could try is, in the __init__.py for CJB, do

from ClassA import ClassA
from ClassB import ClassB

You can then refer to the classes using the names you want.

regards
  Steve
-- 
http://www.holdenweb.com
http://pydish.holdenweb.com
Holden Web LLC +1 800 494 3119




More information about the Python-list mailing list