Next / Previous / Contents / TCC Help System / NM Tech homepage

11.7. class SnailRun: A very small example class

Let's start building a snail-racing application for Malek the object-oriented Python way. Let's assume that all we're tracking about a particular snail is its name and its finishing time. We need to define a class named SnailRun, whose instances track just these two attributes.

Here is the general form of a class declaration in Python:

class ClassName:
    def method1(self, ...):
        block1
    def method2(self, ...):
        block2
    ... etc.

A class declaration starts out with the keyword class, followed by the name of the class you are defining, then a colon (:). The methods of the class follow; each method starts with “def”, just as you use to define a function.

Before we look at the construction of the class, let's see how it works in practice. To create an instance in Python, you use the name of the class as if it were a function call, followed by a list of arguments in parentheses. Our SnailRun constructor method will need two arguments: the snail's name and its finish time. Once we have defined the class, we can build a new instance like this:

judyRace9 = SnailRun ( 'judy', 87.3 )

To get the snail's name and time attributes from an instance, we use the instance name, followed by a dot (.), followed by the attribute name:

>>> judyRace9.name
'judy'
>>> print judyRace9.time
87.3

Our example class, SnailRun, will have just two methods:

Continuing our example from above, here's an example of the use of the .show() method:

>>> print judyRace9.show()
Snail 'judy' finished in 87.3 minutes.

Here is the entire class definition:

class SnailRun:
    def __init__ ( self, snailName, finishTime ):
        self.name  =  snailName
        self.time  =  finishTime

    def show ( self ):
        return ( "Snail '{0}' finished in {1:.1d} minutes.".format(
                 self.name, self.time )

Instantiation means the construction of a new instance. Here is how instantiation works.

  1. Somewhere in a Python program, the programmer starts the construction of a new instance by using the class's name followed by parentheses and a list of arguments. Let's call the arguments (a1, a2, ...).

  2. Python creates a new namespace that will hold the instance's attributes. Inside the constructor, this namespace is referred to as self.

    Important

    The instance is basically a namespace, that is, a container for attribute names and their definitions. For other examples of Python namespaces, see Section 2.2, “The assignment statement”, Section 5.3, “A namespace is like a dictionary”, and Section 9.3, “A module is a namespace”.

  3. The __init__() (constructor) method of the class is executed with the argument list (self, a1, a2, ...).

    Note that if the constructor takes N arguments, the caller passes only the last N-1 arguments to it.

  4. When the constructor method finishes, the instance is returned to the caller. From then one, the caller can refer to some attribute A of the instance I as “A.I”.

Let's look again in more detail at the constructor:

    def __init__ ( self, snailName, finishTime ):
        self.name  =  snailName
        self.time  =  finishTime

All the constructor does is to take the snail's name and finish time and store these values in the instance's namespace under the names .name and .time, respectively.

Note that the constructor method does not (and cannot) include a return statement. The value of self is implicitly returned to the statement that called the constructor.

As for the other methods of a class, their definitions also start with the special argument self that contains the instance namespace. For any method that takes N arguments, the caller passes only the last N-1 arguments to it.

In our example class, the def for the .show() method has one argument named self, but the caller invokes it with no arguments at all:

>>> kyleRace3=SnailRun('Kyle', 96.6)
>>> kyleRace3.show()
"Snail 'Kyle' finished in 96.6 minutes."