7.1. Classes#

A class contains the attributes of an object and its methods and needs to be declared before we can create our objects. In Python a class is declared using the class keyword.

class Class_name:
    attributes

Where Class_name is the name of the class and attributes is a list of the class attributes. Once a class has been declared an object of the class is creates using

object_name = Class_name

The attributes of an object are accessed using

object_name.attribute

Lets create a class for members of the university. Create a new Python file called 7_Object_orientated_programming.py and save it to your OneDrive folder. Enter the following code into your program.

# 7. Object Orientated Programming

# Classes
class Member:
    name = "Joel Miller"
    ID = 12345678
    

joel = Member

print(joel.name)
print(joel.ID)

Here we have declared a class called Member which has two attributes name and ID which we set to a string and an integer. We then create an object called joel and print the values of the attributes. Run your program and the you should see the following added to the console.

Joel Miller
12345678

7.1.1. Changing the attributes#

Lets create another object of our Member class and print out the attributes. Enter the following code into your program.

#  Changing the attributes
ellie = Member

print()
print(ellie.name)
print(ellie.ID)

Run your program and you should see the following added to the console.

Joel Miller
12345678

These details are incorrect for our ellie object. To change the values of the attributes we simply access the attributes of the object and set their values. Enter the following code into your program.

ellie.name = "Ellie Williams"
ellie.ID = "24123456"

print()
print(ellie.name)
print(ellie.ID)

Run your program and you should see the following added to the console.

Ellie Williams
24123456

7.1.2. Constructors#

Changing the attributes whenever we want to create a new object is a bit cumbersome. Fortunately in python we have the __init__() method (a method is a function that belongs to a class) that is called whenever a new object is created. This is known as a constructor is an useful for setting the values of attributes and performing any initialisation routines. The Python syntax for a constructor is

def __init__(self, arguments):
    commands to be executed

Where self is parameter that is the reference to the current instance of the class. Lets declare a constructor to assign the attributes to inputted values and output a message to the console. In your Member class, enter the following code.

def __init__(self, name, ID):
    self.name = name
    self.ID = ID

Here we have added a constructor that takes inputs of name and ID and assigns them to the appropriate attributes. At the end of your program enter the following code.

# Constructors
tommy = Member("Tommy Miller", 87654321)

print()
print(tommy.name)
print(tommy.ID)

We then create a new object tommy with its attributes. Run your program and you should see the following added to the console.

Tommy Miller
87654321

7.1.3. Exercises#

Exercise 7.1

Create a class called Vehicle with a constructor that initialises the attributes name and max_speed. Create the following objects of the Vehicle class and print the values of the attributes.

  1. delorean: name is Delorean and maximum speed is 110

  2. batmobile: name is Batmobile and maximum speed is 330

Exercise 7.2

Create a class called Shape with a constructor that initialises the attributes name and number_of_edges. Create objects for the following shapes and print the values of the attributes.

  1. a triangle

  2. a rectangle

  3. a circle