Question on making names visible in packages

Mark McEahern mark at mceahern.com
Sun Mar 14 20:03:03 EST 2004


Kenneth McDonald wrote:
> I'm writing a package which will have a number of classes. I'd
> like to split them amongst different files within the package.
> However, I'd also like the package user to be able to say:
> 
> 	import Package
> 
> and then to use the classes as 
> 
> 	Package.Class1, Package.Class2, etc.

Example:

D:\BUFFER\Package
     bar.py
     foo.py
     __init__.py

bar defines class Bar; foo defines class Foo.

__init__.py looks like this:

from bar import Bar
from foo import Foo

Now, I can:

   import Package
   f = Package.Foo()

If the contents of Package are volatile, you could even use os, glob, 
and __import__ to make the from x import y entirely dynamic.

// m





More information about the Python-list mailing list