Why a class when there will only be one instance?

SeeBelow at SeeBelow.Nut SeeBelow at SeeBelow.Nut
Tue May 25 21:34:36 EDT 2004


Roy Smith wrote:
> 
> In article <40B3E861.29B033D5 at shaw.ca>, SeeBelow at SeeBelow.Nut wrote:
> 
> > I see the value of a class when two or more instances will be created,
> > but Python programmers regularly use a class when there will only be one
> > instance.
> > What is the benefit of this?  It has a disadvantage of a whole lot of
> > "self."
> > being required everywhere, making the code less readable.  Also, since a
> > strength of Python is rapid application development, it slows one down
> > to have to put in all those self.'s.  The code seems much cleaner to me
> > without classes that have only one instance.  Oh, also, all the methods
> > of this class will have to have the instance name prepended to them.
> >
> > I would appreciate it if someone could explain the advantages of doing
> > this, or at least the sociological reasons why it occurs.
> >
> > Mitchell Timin
> 
> Typing "self" is a mechanical process which adds very little to the
> development cost.  Deciding which things to make classes and which not
> to requires significant mental effort and does add cost.  It's just
> easier to make everything a class.

Even easier is not to make anything a class unless there will be two or
more instances of it.  I still don't get what advantage making a class
buys for you.

> 
> 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?

Other people have mentioned "code reuse".  Again I don't see how a class
helps to make code reusable.  I find methods in a class more difficult
to reuse than simple function definitions.  (unless there are multiple
instances.)

Mitchell Timin

-- 
"Many are stubborn in pursuit of the path they have chosen, few in
pursuit of the goal." - Friedrich Nietzsche

http://annevolve.sourceforge.net is what I'm into nowadays.
Humans may write to me at this address: zenguy at shaw dot ca



More information about the Python-list mailing list