T1W7 Classes & Objects - tstorrnetnz/teaching2025 GitHub Wiki

Introduction

This week we will take a look at a concept that is the foundation of modern computer programming, classes and objects. Some of the ideas in this topic can be quite complicated and subtle, so don't be concerned if it takes awhile to understand it.

At its most simple, classes are a way of creating a custom data type that holds all the data about a "something" e.g. a single person, along with the functions needed to access, change and manipulate the data. An object is the name given to an instance of a class - for example a person called "Trevor".

Object Orientated Programming (OOP) is the name given to programming that uses classes and objects. It is used because it helps ensure that the data and functions of a "thing" do not get confused with the data and functions of other things.

We will look at Classes and Objects over two weeks.

For you to do

Chapter 15 of LPTRW has a reasonable introduction to this topic.

I have made an example that shows how a car class can be used to store data, including the use of setter and getter methods.

Using this repl as a guide:

  1. Create a person class that includes at least five fields (variables), setter and getter methods.
  2. Create three instances (people). Use the setter and getter methods to change the values in your people.
  3. Repeat this for books - have at least five books and include the relevant details. Change the values of some of the books.

Classes and Objects - combining with reading files

Following on from above, we will look a little further at how to use classes and objects. We will learn how to code classes in a separate file, make a list of objects (instances of a class), read a text file into a class and search through the list.

This will mean that you can use a text or comma separated value (CSV) file as your source of data, and load the information into your program in a way that is efficient, safe and easy to access.

Building on cars example above, the list of classes example - run the main.py has a separate module (file) for car, and a list for the cars (listOfCars). Two cars are created and appended to the list. Finally a for loop is used to get the Make values from the car objects in the list, match them to a search criteria, retrieve the car object and print it out.

The cars class from csv is really neat! It uses a csv reader to read the csv file and then create a car object from each line using the car.py and load it into the listOfCars list - all run from main.py

Two references will be useful Geeks for Geeks - reading csv files in python and this stackoverflow post, of course there are many other useful resources for you to find on the internet.

For you to do:

  1. Using the two examples above as a guide, create a csv file of your favourite music - have at least 10 different items and at least 5 different fields e.g. Artist name, year, title etc. Create a program with a separate class for your music and load your csv file into a list of your music, using your music class to hold data about the music. Ensure you have setter and getter fields.
  2. This next problem is a challenge. Using your program in 1 above, write some code to change the value of one of your music objects in your music list. You will need to use the getter and setter methods that you coded. Write back your data to the CSV file - how will you do this?