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).

Work through the following:

A video describing the concept.

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]:

In [3]:

```
zoe
```

Out[3]:

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.**

A video describing the concept.

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]:

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

In [6]:

```
vince = Student()
vince.courses
```

Out[6]:

In [7]:

```
vince.age
```

Out[7]:

In [8]:

```
vince.gender
```

Out[8]:

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

In [9]:

```
vince.age += 1
vince.age
```

Out[9]:

In [10]:

```
vince.gender = 'Male'
vince.gender
```

Out[10]:

In [11]:

```
vince.courses.append('Chemistry')
vince.courses
```

Out[11]:

**Create instances with attributes and experiment with them.**

A video describing the concept.

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]:

In [13]:

```
vince.have_a_birthday(years=10)
vince.age
```

Out[13]:

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]:

In [16]:

```
zoe.courses, zoe.age, zoe.gender
```

Out[16]:

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.**

A video describing the concept.

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]:

In [19]:

```
geraint.have_a_birthday(8)
geraint.age
```

Out[19]:

In [20]:

```
geraint.complain()
```

Out[20]:

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

A video describing the concept.

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
def __add__(self, linexp):
"""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]:

In [23]:

```
exp.a
```

Out[23]:

In [24]:

```
exp.b
```

Out[24]:

In [25]:

```
exp.root()
```

Out[25]:

In [26]:

```
exp2 = LinearExpression(5, -2)
exp2
```

Out[26]:

In [27]:

```
exp + exp2 # This works because of the `__add__` method
```

Out[27]:

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]:

In [29]:

```
exp3.root()
```

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
def __add__(self, linexp):
"""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]:

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]:

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]:

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

**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
```

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.

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.)

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:

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