Chapter 8. Classes

In mathematics, when we write sin, we refer to a mathematical object for which we know many methods from elementary calculus. For example:

  • We might want to evaluate sin x at x=0.5, that is, compute sin(0.5), which returns a real number
  • We might want to compute its derivative, which gives us another mathematical object, cos
  • We might want to compute the first three coefficients of its Taylor polynomial

These methods may be applied not only to sin but also to other sufficiently smooth functions. There are, however, other mathematical objects (for example, the number 5) for which these methods make no sense. Objects that have the same methods are grouped together in abstract classes, for example, functions. Every statement and every method that can be applied to functions applies in particular to sin or cos. Other examples for such classes might be a rational number, for which a denominator and numerator method exist; an interval, which has a left and right boundary method; an infinite sequence, for which we can ask whether it has a limit, and so on.

In this case, sin is called an instance of the class. The mathematical phrase Let g be a function... is, in this context, called instantiation. Here, g is the name of the function; one of many attributes that can be assigned to it. Another attribute might be its domain.

The mathematical object p(x) = 2x2- 5 is just like the sine function. Every function method applies to p, but we can also define special methods for p. We might, for instance, ask for p’s coefficients. These methods can be used to define the class of polynomials. As polynomials are functions, they additionally inherit all methods of the function class.

In mathematics, we often use the same operator symbol for completely different operations. For instance, in  5+4 and sin + cos, the operator symbol + has different meanings. By using the same symbol, one tries to express the similarities of mathematical operations. We have introduced these terms from object-oriented programming by applying them to mathematical examples:

  • Classes
  • Instance and instantiation
  • Inheritance
  • Methods
  • Attributes
  • Operator overloading

In this chapter, we will show how these concepts are used in Python.

Introduction to classes

We will illustrate the concept of classes with an example of rational numbers, that is, numbers of the form q= qN ⁄ qD, where qN and qD are integers.

Introduction to classes

Figure 8.1: An example of a class declaration

We use rational numbers here only as an example for the class concept. For future work in Python with rational numbers use the fractions module (refer to [6]).

Class syntax

The definition of a class is made by a block command with the class keyword, the name of the class, and some statements in the block (refer to Figure 8.1):

class RationalNumber: 
      pass

An instance of this class (or in other words, an object of the type RationalNumber) is created by

r = RationalNumber()

and a query type(a) returns the answer,  <class'__main__.RationalNumber'>. If we want to investigate whether an object is an instance of this class, we can use this:

if isinstance(a, RationalNumber):
    print('Indeed it belongs to the class RationalNumber')  

So far we've generated an object of the RationalNumber type, which has no data yet. Furthermore, there are no methods defined to perform operations with these objects. This will be the subject of the next sections.

The __init__ method

Now we provide our example class with some attributes; that is, we give it defining data. In our case, this data will be the values of the denominator and the numerator. To this end, we have to define a method, __init__, used to initialize the class with these values:

class RationalNumber:
    def __init__(self, numerator, denominator):
        self.numerator = numerator
        self.denominator = denominator

Before we explain the special __init__ function, which we added to the class, we demonstrate the instantiation of a RationalNumber object:

q = RationalNumber(10, 20)    # Defines a new object
q.numerator    # returns 10
q.denominator    # returns 20

A new object of type RationalNumber is created by using the class name as if it was a function. This statement does two things:

  • It first creates an empty object, q.
  • Then it applies the __init__ function to it; that is, q.__init__(10, 20) is executed.

The first parameter of __init__ refers to the new object itself. On function call, this first parameter is replaced by the object’s instance. This applies to all methods of the class and not only to the special method __init__. The special role of this first parameter is reflected by the convention to name it self. In the  previous example, the __init__ function defines two attributes of the new object, numerator and denominator.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.76.204