Organizing Code - Packages

Bruno Desthuilliers bruno.42.desthuilliers at wtf.websiteburo.oops.com
Fri Sep 7 11:42:03 EDT 2007


xkenneth a écrit :
> All,
> 
>     I apologize if this is a commonly asked question, but I didn't
> find anything that answered my question while searching.
> 
> So what I have right now is a few packages that contain some commonly
> used functions and another package that contains all of my custom
> error classes. I want these error classes available to me in all of
> the other packages in my library. Currently to achieve this at the top
> of every module file I have the line "from My.Library.Errors import
> *", my problem with this is that it manages to import the Errors into
> every scope that they are used. 

Ain't that what you want ??? Having "these error classes available to me 
in all of the other packages in my library" ?

If you're worried about perfs or whatever, don't worry, a module is only 
imported once - next imports will only bind the names in the importing 
namespace.

> I'm still pretty new to Python, and my
> approachs are probably very rooted in C/C++ (I've had the hardest time
> getting over not being able to overload functions), but am I doing
> this correctly?

Yes, that's the right thing to do.

>    Also, are there any good tutorials/examples out there of how to
> organize your python code into packges?

Most of the Python-specific aspects should be covered by the tutorial 
(the one in the doc). Else, it's as usual, trying to have high cohesion 
and low coupling.

Ah, yes, a couple of things:
- avoid the 'one-class-per-file' syndrom. It's perfectly ok to have tens 
of classes in a same module
- plain functions are ok too - no need to stick everything in classes.

HTH



More information about the Python-list mailing list