[Tutor] Importing multiple files as a single module?

Kent Johnson kent37 at tds.net
Fri Jan 21 17:53:35 CET 2005


I think this will work:
in foo/__init__.py put
from Bar import Bar
from Baz import Baz

or whatever variations of this you like.

Any names defined in the package __init__.py are available to other code as package attribute.

Kent

Max Noel wrote:
> Hi everyone,
> 
>     Having learnt OOP with C++, and most of my OOP experience being with 
> Java, I'm used to storing my classes in one file for each class. I find 
> it tidier and easier to read/debug.
>     However, when I try to do the same in Python, each file corresponds 
> to a module with a single class in it.
> 
>     Is there a way to obtain the same result in Python as in Java? That 
> is, I'd like to have the following on my hard drive:
> 
> foo/
>     Bar.py
>     Baz.py
> 
>     Where Bar.py and Baz.py each contain a single class (Bar and Baz). 
> Then, from Python, I'd import the foo module (import foo) and then 
> access the classes with foo.Bar and foo.Baz (instead of foo.Bar.Bar and 
> foo.Baz.Baz, which is what I have now). Being able to use Bar in Baz.py 
> would of course be a nice side effect.
> 
>     Any ideas? Or am I trying to do things in a very non-Pythonic way? 
> If so, how am I supposed to organize my OOP code, especially given the 
> absence of a "real" IDE for Python?
> 
> Thanks for your attention,
> -- Max
> maxnoel_fr at yahoo dot fr -- ICQ #85274019
> "Look at you hacker... A pathetic creature of meat and bone, panting and 
> sweating as you run through my corridors... How can you challenge a 
> perfect, immortal machine?"
> 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
> 



More information about the Tutor mailing list