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

17. Create your own types: The class construct

You can define new types, or classes, with Python's class declaration. Some definitions:

class

A type (built-in or user-defined).

object

One value of a given type. As the number 23 is an object of type int, so an object in general is one instance of a user-defined type. It is useful to think of a class a cookie cutter (that is, a pattern), and an object as a cookie that follows that pattern. Like individual cookies, individual objects all start out looking the same, but inside them are values that may change.

The type or class defines the behaviors common to all objects of the same type. For example, the unary - operator works on all values of type float, and changes the sign of the value.

instance

Same as “object.”

member

One of the values inside an instance. For example, a complex number C has two float values inside it: the real pattern is referred to as C.real and the imaginary part as C.imag. Ultimately all the values inside an instance are the atomic types listed in the table of basic types, or other objects that themselves contain atomic types, and so on.

method

A function that operates primarily on an object. Unlike regular Python functions, which are called with the function name (such as “sqrt(x)”), to call a method you use the syntax:

O.m(...)

where O is an object, m is the method name, and you supply zero or more arguments between parentheses.

To define a new class of objects:

class C:
    Bi
    def n0 ( self, ... ):
        B0
    def n1 ( self, ... ):
        B1
    ...

This construct defines a new class C.

To define a derived class that inherits from superclasses P0, P1, ...:

class C(P0,P1,...):

You can inherit from built-in classes such as int and dict. See below under the class constructor for more details.

You can use a number of special method names to define certain behaviors of your objects. All these names start and end with two underbars (__). The most commonly used one is __init__(), the class constructor, but there are many more to define how your objects act when operated on by various operators.

17.1. __init__(): The class constructor

The constructor is a method that is executed to create a new object of the class. Its name is always __init__().

Its parameter list must always include self as the first argument; any remaining parameters must be provided by the caller. For example, if your constructor has four parameters including self, a caller must provide three arguments.

You can think of self as representing the name space inside the instance. To add new members to an instance, just assign a value to self.v where v is the member name.

Here's a complete example of a class FeetInches that represents dimensions in feet and inches. Internally the dimension is represented as only inches, and that value is stored in a member named .__inches (member names starting with two underbars, like this, one, are private members, and hence accessible only from inside the object).

The constructor takes two arguments named feet and inches and converts them to the private member .__inches. Then we define a method called .show() that converts the internal dimension to a string of the form f ft i in where f is the whole feet and i is the inches part. Here's the class definition:

class FeetInches:
    def __init__ ( self, feet, inches ):
        self.__inches  =  (feet * 12.0) + inches

    def show(self):
        feet  =  int ( self.__inches / 12.0 )
        inches  =  self.__inches - feet * 12.0
        return "%d ft %.2f in" % (feet, inches)

To create an object of this type we might say:

fiveFootFour = FeetInches(5, 4)

The method call fiveFootFour.show() would then yield the string "5 ft 4.00 in".