package question: getting names one level deep?

Russell E. Owen owen at astrono.spamwashington.emu
Fri Feb 23 11:41:26 EST 2001


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



More information about the Python-list mailing list