Python Classes and Objects


Object-oriented Python Programming

Python is a full-featured object-oriented programming language, with features such as classes, inheritance, objects, and overloading. Python does not force the programmer to write object-oriented code, but the programmer who avoids Python's object-oriented features is missing much of Python's power.

Defining Classes

The syntax is:

class <class_name>[(<parent-class-name>)]:
   [<class-variable>=<initial-value>]
         :
   [self.<object-variable>=<initial-value>]
         :
   def __init__(self):
      <statements>

   [<function-definitions>]

If variables are prefixed with self., then each object of the class will have its own copy of the variable. If not, the variable belongs to the class itself, and there is only one copy of the variable created.

The __init__(self) function is the constructor, and self refers to the object iself, similar to this in C++. The parent-class name and its parentheses are required if the class is to inherit from another class. Note that __init__ is surrounded by pairs of underscores.

An Example Class Definitions:

These are classes used in the game, Hunt the Wumpus. In the game, there are two kinds of monster: the wumpus and the super bats. Here, the Beast class represents properties of both kinds of monster, while the Wumpus_class inherits Beast properties and provides functions peculiar to the Wumpus.

class Beast:
   #Keep track of count and locations of beasts (bats and wumpus).
   #These variables belong to the class, not to individual beasts.
   beast_count = 0
   beast_loc = []

   def __init__(self):
      #Place the beast.
      self.loc = ro.randint(0, 19)
      #If there is already a beast in that location, try again.
      while self.loc in Beast.beast_loc:
         self.loc = ro.randint(0, 19)
      #Count the new beast, and remember where it is.
      Beast.beast_count = Beast.beast_count + 1
      Beast.beast_loc.append(self.loc)

   def get_loc(self):
      return self.loc

   def set_loc(self):
      self.loc = ro.randint(0, 19)

class Wumpus_class(Beast):

   def move(self, cave):
   #Wumpus moves to an adjoining room at random.
      self.loc = cave.get_adj_rooms(self.loc)[ro.randint(0, 2)]

   def wake(self, cave):
   #Someone has awakened the wumpus. 25% of such times it should move to an
   #adjoining room.
      if ro.random() > .25:
         self.move(cave)

Note the parameter mismatch between the function call self.move(cave) in the last line, and in the function definition, def move(self, cave) eight lines above. This is not an error; self is not used as a parameter in the function call because appears before the "dot" in that statement.


Instantiating Objects

To instantiate an object, call the class, passing the constructor parameters, assigning the returned value to the name of the object:

<object-name>=<class-name>([<constructor-parameters>])

An Example Instantiation:

wumpus = Wumpus_class()

There is no parameter in the Wumpus_class() because the Beast constructor function takes only self as a parameter. Note that the instantiation statement must be located below the class definition in the program file.

To see the entire Hunt the Wumpus program, click here.

Next: Python Scripting

Go back to previous page.

Go back to first Python page.