package question: getting names one level deep?

Robin Becker robin at jessikat.fsnet.co.uk
Fri Feb 23 15:00:53 EST 2001


In article <owen-2302010841550001 at cs205-86.fsmodem.washington.edu>,
Russell E. Owen <owen at astrono.spamwashington.emu> writes

one way is to put code into the __init__.py

eg

from classa import classa
from classb import classb

then classa, classb become visible as attributes of the package and you
can do
>>> from apackage import classa
>>> classa
<class apackage.classa.classa at 014C5FCC>

>I like to break my code up into lots of files -- typically one file contains a 
>class and turn a related set of files/classes into a package. When a file 
>contains a class, naturally I give the file the same name as the class.
>
>However, I've not found a nice way to refer to the resulting classes. I hope I'm 
>missing some easy, standard trick.
>
>Say I have a directory structure such as:
>apackage/
>  __init__.py  which contains __all__ = [list of all files]
>  classa.py
>  classb.py
>  ...
>
>To use these classes I write:
>
>import apackage
>x = package.classa.classa(etc)
>
>It's that duplicated file-name.identical-class-name that I'd like to eliminate, 
>so I could say:
>
>import apackage (or something)
>x = apackage.classa(etc)
>
>Basically I'd like to do an import * into the "apackage" namespace, and ideally 
>I'd like "import apackage" to somehow magically do it. In other words, ideally 
>I'd like to set up my packages so they act as if all their code (at least all 
>intended-to-be-visible-to-the-outside-world code) was in a single module.
>
>Any hints?
>
>-- Russell

-- 
Robin Becker



More information about the Python-list mailing list