Why a class when there will only be one instance?

Roy Smith roy at panix.com
Tue May 25 21:55:42 EDT 2004


In article <40B3F448.EB711333 at shaw.ca>, SeeBelow at SeeBelow.Nut wrote:

> > More than that, most times I've decided to not bother making something a
> > class because it was too simple, I've eventually added enough
> > functionality to it to change my mind and have to re-do things.  That's
> > real cost.  Much simplier and cheaper to just make it a class from the
> > get-go.
> 
> Why does greater functionality make a class desireable, if there won't
> be multiple instances created?

For me, it's more about encapsulation than code re-use.  If I've got a 
bunch of functions which operate on a collection of data, to me that 
says "object", which in Python (and most OOPL's) implies "class".

Bundling it up into a class lets me think about it as a unit.  Each 
class is a convenient thought unit for design and testing, and also for 
understanding somebody else's code.

I don't think there's a need to be dogmatic about it.  It's just what I 
find is a natural way to break a program down into smaller pieces you 
can get your brain around one piece at a time.



More information about the Python-list mailing list