Why a class when there will only be one instance?

Ryan Paul segphault at sbcglobal.net
Tue May 25 21:05:35 EDT 2004


On Wed, 26 May 2004 00:43:49 +0000, SeeBelow 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

defining a class may be useful if you plan on making more instances down
the line. It's a good OO strategy. I do understand your dislike of 'self'.
It does seem like clutter. In my code, I shorten it to 's'. In ruby, class
variables are prefixed with an '@', which makes them easier to discern in
code, and it is easier than typing 'self'. I wish python had something
like that. I also think that having to specify the class variable in every
function definition is a bit silly. Ruby gets rid of that too.

--SegPhault




More information about the Python-list mailing list