Why a class when there will only be one instance?

Roy Smith roy at panix.com
Tue May 25 21:12:13 EDT 2004


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.

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.



More information about the Python-list mailing list