## Chapter 04: Object Oriented Programming¶

This lab sheet introduces object oriented programming. We have been using object oriented programming throughout the previous lab sheets. Here we will learn how to create our own objects (what this means become clear).

## Tutorial¶

Work through the following:

### 1. Writing a class.¶

A video describing the concept.

A video demo.

The main idea behind object orientated programming (OOP) is to create abstract structures that allow us to not worry about data. Alan Kay came up with the concept and is quoted as saying: ‘I wanted to get rid of data’. Instead of keeping track of variables using lists and arrays and writing specific functions for each operation we could be trying to do we use a system similar to the cellular structure in biology:

Here is an image showing the various things we will consider in this lab sheet:

Creating a class in Python is similar to creating a function: we write down the rules:

In [1]:
class Student:
"""A base class"""


We can then create 'instances' of this class:

In [2]:
vince = Student()
zoe = Student()
vince

Out[2]:
<__main__.Student at 0x7f54c811a9e8>
In [3]:
zoe

Out[3]:
<__main__.Student at 0x7f54c811a9b0>

The at ... is a pointer to the location of the instance in memory. If you re run the code that location will change.

We have already seen examples of classes in Python:

• Integers;
• Strings;
• Lists.

There are many more and we are going to see how to build our own.

Experiment with building an instance of the Student class.

### 2. Attributes¶

A video describing the concept.

A video demo.

The above student class is not very useful. We now see how to make our objects ‘hold’ information. The following code re-creates our previous student class but gives the class some 'attributes':

In [4]:
class Student:
courses = ['Biology', 'Mathematics', 'English']
age = 12
gender = "Female"


Now our class itself has some information:

In [5]:
Student.age

Out[5]:
12

This information is also passed on to any instances of the class:

In [6]:
vince = Student()
vince.courses

Out[6]:
['Biology', 'Mathematics', 'English']
In [7]:
vince.age

Out[7]:
12
In [8]:
vince.gender

Out[8]:
'Female'

We can use and/or modify those attributes just like any other Python variable:

In [9]:
vince.age += 1
vince.age

Out[9]:
13
In [10]:
vince.gender = 'Male'
vince.gender

Out[10]:
'Male'
In [11]:
vince.courses.append('Chemistry')
vince.courses

Out[11]:
['Biology', 'Mathematics', 'English', 'Chemistry']

Create instances with attributes and experiment with them.

### 3. Methods¶

A video describing the concept.

A video demo.

We will now see how to make classes 'do' things. These are called 'methods' and they are just functions 'attached' to classes.

In [12]:
class Student:
"""A class to represent a student"""
courses = ['Biology', 'Mathematics', 'English']
age = 12
gender = "Female"
def have_a_birthday(self, years=1):
"""Increment the age"""
self.age += years  # self corresponds to the instance
vince = Student()
vince.have_a_birthday()
vince.age

Out[12]:
13
In [13]:
vince.have_a_birthday(years=10)
vince.age

Out[13]:
23

There are various 'special' methods names that act in particular ways. One of these is __init__, this method is called when an instance is created ('initialised'):

In [14]:
class Student:
"""A class to represent a student"""
def __init__(self, courses, age, gender):
self.courses = courses
self.age = age
self.gender = gender
def have_a_birthday(self, years=1):
"""Increment the age"""
self.age += years  # self corresponds to the instance


Now we can easily create instances with given attributes:

In [15]:
vince = Student(["Maths"], 32, "Male")
zoe = Student(["Biology"], 31, "Female")
vince.courses, vince.age, vince.gender

Out[15]:
(['Maths'], 32, 'Male')
In [16]:
zoe.courses, zoe.age, zoe.gender

Out[16]:
(['Biology'], 31, 'Female')

There are various other 'special' methods, we will see one of them in the worked example.

Create instances of the Student class with these new methods.

### 4. Inheritance¶

A video describing the concept.

A video demo.

One final (very important) aspect of object oriented programming is the concept of inheritance. This allows us to create new classes from other ones. In practice this saves replicating code as we can change only certain methods as required.

To do this we simply create the new class as usual but pass it the old class:

class NewClass(OldClass):
...


For example, let us create a student who we know is born on the 29th of February (a date that only occurs once every 4 years):

In [17]:
class LeapYearStudent(Student):
"""A class for a student born on the 29th of February"""
# Note that we do not have to rewrite the init method
def have_a_birthday(self, years=1):
self.age += int(years / 4)
def complain(self):
"""Return a string complaining about birthday"""
# This is a new method that the Student class does not have
return "I wish I was not born on the 29th of Feb"


Here is how this new class behaves:

In [18]:
geraint = LeapYearStudent(["Maths"], 22, "Male")
geraint.have_a_birthday()
geraint.age  # Still  22

Out[18]:
22
In [19]:
geraint.have_a_birthday(8)
geraint.age

Out[19]:
24
In [20]:
geraint.complain()

Out[20]:
'I wish I was not born on the 29th of Feb'

Experiment with the above code: how would it work if leap year was every 3 years?

### Worked example¶

A video describing the concept.

A video demo.

Let us assume we want to study linear expressions. These are expressions of the form:

$$ax+b$$

We are interested, for example, in what the value of $x$ for which a linear expression is 0. This is called the 'root' and is the solution to the following equation:

$$ax+b=0$$

This is obviously an easy thing to study but we're going to assume it's not and build a class to represent and manipulate linear expressions.

In [21]:
class LinearExpression:
"""A class for a linear expression"""
def __init__(self, a, b):
self.a = a
self.b = b

def root(self):
"""Return the root of the linear expression"""
return - self.b / self.a

"""A special method: lets us have addition between expressions"""
return LinearExpression(self.a + linexp.a, self.b + linexp.b)

def __repr__(self):
"""A special method: changes the way an instance is displayed"""
return "Linear expression: " + str(self.a) + "x + " + str(self.b)

In [22]:
exp = LinearExpression(2, 4)
exp  # This output is given by the __repr__ method

Out[22]:
Linear expression: 2x + 4
In [23]:
exp.a

Out[23]:
2
In [24]:
exp.b

Out[24]:
4
In [25]:
exp.root()

Out[25]:
-2.0
In [26]:
exp2 = LinearExpression(5, -2)
exp2

Out[26]:
Linear expression: 5x + -2
In [27]:
exp + exp2  # This works because of the __add__ method

Out[27]:
Linear expression: 7x + 2

This class works just fine but we quickly arrive at a problem:

In [28]:
exp1 = LinearExpression(2, 4)
exp2 = LinearExpression(-2, 4)
exp3 = exp1 + exp2
exp3

Out[28]:
Linear expression: 0x + 8
In [29]:
exp3.root()

---------------------------------------------------------------------------
ZeroDivisionError                         Traceback (most recent call last)
<ipython-input-29-78f38426a60e> in <module>()
----> 1 exp3.root()

<ipython-input-21-5c37504e78d2> in root(self)
7     def root(self):
8         """Return the root of the linear expression"""
----> 9         return - self.b / self.a
10

ZeroDivisionError: division by zero

We get an error because our root method is attempting to divide by 0. Let's fix that:

In [30]:
class LinearExpression:
"""A class for a linear expression"""
def __init__(self, a, b):
self.a = a
self.b = b

def root(self):
"""Return the root of the linear expression"""
if self.a != 0:
return - self.b / self.a
return False

"""A special method: let's us have addition between expressions"""
return LinearExpression(self.a + linexp.a, self.b + linexp.b)

def __repr__(self):
"""A special method: changes the default way an instance is displayed"""
return "Linear expression: " + str(self.a) + "x + " + str(self.b)

In [31]:
exp3 = LinearExpression(0, 8)
exp3.root()

Out[31]:
False

Let us now use this to verify the following simple fact. If $f(x) = a_1x+b_1$ and $g(x) = a_2x+b_2$, then the root of $f(x) + g(x)$ is given by:

$$\frac{a_1x_1 + a_2x_2}{a_1+a_2}$$

where $x_1$ is the root of $f$ and $x_2$ is the root of $g$ (if they exist).

First let's write a function that checks this for a given set of $a_1, a_2, b_1, b_2$.

In [32]:
def check_result(a1, a2, b1, b2):
"""Check that the relationship holds"""
f = LinearExpression(a1, b1)
g = LinearExpression(a2, b2)
k = f + g
x1 = f.root()
x2 = g.root()
x3 = k.root()
if (x1 is not False) and (x2 is not False) and (x3 is not False):
# Assuming our three expressions have a root
return (a1 * x1 + a2 * x2) / (a1 + a2) == x3
return True  # If f, g have no roots the relationship is still true
check_result(2, 3, 4, 5)

Out[32]:
True

We will verify this by randomly sampling values for $a_1, a_2, b_1, b_2$.

In [33]:
import random  # Importing the random module
N = 1000  # The number of samples
checks = []
for _ in range(N):
a1 = random.randint(-10, 10)
a2 = random.randint(-10, 10)
b1 = random.randint(-10, 10)
b2 = random.randint(-10, 10)
checks.append(check_result(a1, a2, b1, b2))
all(checks)

Out[33]:
True

## Exercises¶

Here are a number of exercises that are possible to carry out using the code concepts discussed:

• Classes: creating a set of rules that describe an abstract "thing"
• Attributes: variables on a class
• Methods: functions on a class
• Inheritance: creating new classes from old classes

### Exercise 1¶

Debugging exercise

The following is an attempt to write a class for rectangle, find and fix all the bugs.

class Rectangle:
"""A class for a rectangle""

def __init__(width, length)
self.width = width
self.length = width

def obtain_area(self:
"""Obtain the area of the rectangle"""
return self.width * self.length

def is_square():
"""Check if the rectangle is a square"""
return self.width == self.length


### Exercise 2¶

Build a class for a quadratic expression:

$$ax^2+bx+c$$

Include a method for adding two quadratic expressions together and also a method to calculate the roots of the expression.

### Exercise 3¶

If rain drops were to fall randomly on a square of side length $2r$ the probability of the drops landing in an inscribed circle of radius $r$ would be given by:

$$P = \frac{\text{Area of circle}}{\text{Area of square}}=\frac{\pi r ^2}{4r^2}=\frac{\pi}{4}$$

Thus, if we can approximate $P$ then we can approximate $\pi$ as $4P$. In this question we will write code to approximate $P$ using the random library.

First of all, create a class for a rain drop (make sure you understand the code!):

class Drop():
def __init__(self, r=1):
self.x = (.5 - random.random()) * 2 * r
self.y = (.5 - random.random()) * 2 * r
self.incircle = (self.y) ** 2 + (self.x) ** 2 <= (r) ** 2


Note that the above uses the following equation for a circle centred at $(0,0)$ of radius $r$:

$$x^2+y^2≤r^2$$

To approximate $P$ simply create $N=1000$ instances of Drops and count the number of those that are in the circle. Use this to approximate $\pi$.

(This is an example of a technique called Monte Carlo Simulation.)

### Exercise 4¶

In a similar fashion to question 8, approximate the integral $\int_{0}^11-x^2\;dx$.

Recall that the integral corresponds to the area under a curve. Furthermore this diagram might be helpful:

Previous

Next

Source code: @drvinceknight Powered by: Python Jupyter Mathjax Github pages Skeleton css