Why a class when there will only be one instance?

Grant Edwards grante at visi.com
Wed May 26 11:32:11 EDT 2004


On 2004-05-26, SeeBelow at SeeBelow.Nut <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?

 1) Because people are notoriously bad and predicting the
    future. Their guesses at what "will be" are often wrong.

    If you use a class, then you can add a second instance
    later when you figure out what the customer really wanted.
    
 2) I find it often helps me think through the problem and
    arrive at a more structured, easily maintained solution
    compared to a bunch of random functions.

> It has a disadvantage of a whole lot of "self." being required
> everywhere, making the code less readable.

In some cases (particulary mathematical computations), the
self's can reduce readability. A few local variables can fix
that.

> Also, since a strength of Python is rapid application
> development, it slows one down to have to put in all those
> self.'s.

I've never seen any project where the speed of development was
limited by typing speed.

> The code seems much cleaner to me without classes that have
> only one instance.

Until the requirements change (or are actually discovered), and
you need more than one instance.

-- 
Grant Edwards                   grante             Yow!  If I felt any more
                                  at               SOPHISTICATED I would DIE
                               visi.com            of EMBARRASSMENT!



More information about the Python-list mailing list