Why a class when there will only be one instance?

Jeffrey Barish jeffbarish at starband.net
Wed May 26 12:03:26 EDT 2004


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
> 
As a novice Python programmer, I also thought about this question
recently and came up with one other rationale that no one else has
mentioned yet.  (If this rationale is wrong, I would value
elucidation.)  An instance also retains state whereas a function does
not (because the instance continues to exist).  For example, if you
create the function

def assigner():
    x = 2

the variable x exists only when test runs.  A second function printer()

def printer():
    print x

will not print the value assigned to x in assigner.  (You get a
traceback because global variable x is not defined.)  If you want x to
continue to exist after assigner runs, you must declare it global. 
Thus, with

def assigner():
    global x
    x = 2

printer() will print the value assigned to x in assigner.  When assigner
is a method, you can cause x to continue to exist by prepending self. 
All variables with self prepended have global scope within the
instance.  Thus,

class tester:
    def assigner(self):
        x = 1
    def printer(self):
        print x  # global variable x not defined
t = tester()
t.assigner()
t.printer()

will not work (for the same reason it does not work with the functions
above), whereas

class tester:
    def assigner(self):
        self.x = 1
    def printer(self):
        print self.x  # self.x is global within scope of instance
t = tester()
t.assigner()
t.printer()

prints 1.

It works to have a bunch of global variables in a module rather than a
single-instance class, but you may have to be more careful about name
clashes as the code expands.  Using a single-instance class provides
another level of control over scope.  Thus, you could have more than
one single-instance class in a module each defining its own namespace
whereas the code that lives in the other classes would otherwise have
to go in separate modules to provide the same degree of namespace
isolation.  Also, with global declarations, it's hard to tell when you
read the code whether a variable has been declared global, whereas the
"self." prefix essentially announces that the corresponding variable
has been declared to have global scope within the instance.
-- 
Jeffrey Barish





More information about the Python-list mailing list