One module per class, bad idea?

Carl J. Van Arsdall cvanarsdall at mvista.com
Tue Dec 12 18:25:50 EST 2006


Carl Banks wrote:
> Carl J. Van Arsdall wrote:
>   
>> Isaac Rodriguez wrote:
>>     
>>>> Yes, it would be a bad idea. =)
>>>>
>>>>         
>>> Saying it is a bad idea and not explaining why will not help anyone. I
>>> would like you to elaborate on why it is a bad idea to have one file
>>> per class.
>>>
>>>       
>> A module per class makes a lot of sense in some cases, or rather, make
>> your module your class (look at the singleton pattern).  I actually like
>> to structure all of my code like this, it helps me keep things organized
>> and separated.
>>     
>
> I don't understand.  Are you saying you organize your code to be a
> bunch of modules masquerading as singleton classes?  (When I was a
> young'n, we called that "procedure oriented programming" :)
>   
Well, when you have a class you want instantiated once and only once 
(i.e. a singleton) you can do it in python really easily vs a langauge 
like C++ (and i'll explain).

So, in C++ when you want to create a singleton you need to go through a 
little bit of work to make sure that the constructor (this might be a 
naive method, but i never claimed to be elite) checks to make sure 
another class of the same type has never been created.  Singletons get 
more elaborate than this, but I'm just trying to illustrate a point.

In python a model can be treated as a singleton class.  Think of a 
module's global variables as the variables of the class and the function 
in the module that operate on those variables as the class' methods.  
When you instantiate a module you get one and only one, the global 
variables more or less stay static throughout your execution (or at 
least, the way I use it).  So say you have a module that acts as a 
centralized ressource (how about some piece of IO)?  In a language like 
java you will have one and only one interface to that IO (i mean it 
depends on the IO, bear with me its just to illustrate a point), if a 
user tries to make a new class it will just return a reference to the 
singleton class that already exists.  So python modules are really 
useful for this type of construct.

I'm not saying you want to make all of your modules singleton classes, 
but its kind of a hidden benefit of modules.  You get a singleton and 
don't have to really do any extra work :)  Again, i've never done 
anything incredibly complex, but i've found this useful a couple of times.


>> I guess i'm not sure why it would ever be a really bad
>> idea, maybe if you had really small classes?
>>     
>
> Or maybe you have really big classes.
>
> The potential problem with one module per class is one of missed
> opportunity: namely the missed opportunity for a higher-level
> organization.
>
> Classes are rarely, probably never, a good way to organize code at the
> highest levels.  It's better to organize code into subsystems of some
> sort.  It's very rare that each class is an independent subsystem unto
> itself; most high-level subsystems would encompass many classes (as
> well as other code).  When you strictly obey a one class per module
> rule, you lose the possibility of using the module as a means of
> organizing subsystems (and subsubsystems, and so on).
>
> Now, I think this is the best way to use modules, but you don't need to
> use modules to do get higher-level organization; you could use packages
> instead.  It's a pain if you're working on two different classes in the
> same system you have to keep switching files; but I guess some people
> prefer to switch files rather than to scroll for some reason.
>   
Yea, you have a good point.  I don't have a lot of experience with 
packages, but I've also never written anything so large that i've had 
more than 5-10 modules.  I'll spend some time looking into it, thanks!

> I'd say as long as you use package system, and not just a flat
> modulespace, it's not fundamentally disorganized to use a one class per
> module rule.  In the end, it probably comes down to what you prefer,
> but I think most people would prefer not to obey a one class per module
> rule.
>
>
> Carl Banks
>
>   
-Carl V.

-- 

Carl J. Van Arsdall
cvanarsdall at mvista.com
Build and Release
MontaVista Software




More information about the Python-list mailing list