The notion of object depends on having instance variables (or
“attributes”) which have unique values for each object. We
can extend this concept to include variables that are not unique to each
instance, but shared by every instance of the class. Class level
variables are created in the class definition itself; instance variables
are created in the individual class method functions (usually
__init__).
Class level variables are usually "variables" with values that
don't change; these are sometimes called manifest constants or named
constants. In Python, there's no formal declaration for a named
constant.
A class level variable that changes will be altered for all
instances of the class. This use of class-level variables is often
confusing to readers of your program. Class-level variables with state
changes need a complete explanation.
This is an example of the more usual approach with class-level
constants. These are variables whose values don't vary; instead, they
exist to clarify and name certain values or codes.
Example 22.5. wheel.py
import random
class Wheel( object ):
"""Simulate a roulette wheel."""
green, red, black= 0, 1, 2
redSet= [1,3,5,7,9,12,14,16,18,19,21,23,25,27,30,32, 34,36]
def __init__( self ):
self.lastSpin= ( None, None )
def spin( self ):
"""spin() -> ( number, color )
Spin a roulette wheel, return the number and color."""
n= random.randrange(38)
if n in [ 0, 37 ]: n, color= 0, Wheel.green
elif n in Wheel.redSet: color= Wheel.red
else: color= Wheel.black
self.lastSpin= ( n, color )
return self.lastSpin
Part of definition of the class
Wheel includes some class variables.
These variables are used by all instances of the class. By
defining three variables, green,
red and black, we can make
our programs somewhat more clear. Other parts of our program
that use the Wheel class can then
reference the colors by name, instead of by an obscure numeric
code. A program would use Wheel.green to
refer to the code for green within the
Wheel class.
The Wheel class also creates a
class-level variable called redSet. This is
the set of red positions on the Roulette wheel. This is defined
at the class level because it does not change, and there is no
benefit to having a unique copy within each instance of
Wheel.
The __init__ method creates an
instance variable called lastSpin. If we had
multiple wheel objects, each would have a unique value for
lastSpin. They all would all, however, share
a common definition of green,
red, black and
redSet.
The spin method updates the state of
the wheel. Notice that the class level variables are referenced
with the class name: Wheel.green. The
instance level variables are referenced with the instance
parameter: self.lastSpin. The class level
variables are also available using the instance parameter,
Wheel.green is the same object as
self.green.
The spin method determines a random
number between 0 and 37. The numbers 0 and 37 are treated as 0
and 00, with a color of green; a number in the
Wheel.redSet is red, and any other number is
black. We also update the state of the
Wheel by setting
self.lastSpin. Finally, the
spin method returns a
tuple with the number and the code for
the color. Note that we can't easily tell 0 from 00 with this
particular class definition.
The following program uses this Wheel class
definition. It uses the class-level variables red and
black to clarify the color code that is returned by
spin.
w= Wheel()
n,c= w.spin()
if c == Wheel.red: print n, "red"
elif c == Wheel.black: print n, "black"
else: print n
Our sample program creates an instance of
Wheel, called w. The program
calls the spin method of
Wheel, which updates
w.lastSpin and returns the tuple that contains the
number and color. We use multiple assignment to separate the two parts
of the tuple. We can then use the class-level variables to decode the
color. If the color is Wheel.red, we can print
"red".
Published under the terms of the Open Publication License