Introduction

As programs become larger and more complex, organising code clearly and efficiently becomes increasingly important. In earlier Python programs, we often use variables and functions to store data and perform actions. While this works well for small programs, it can quickly become difficult to manage as the program grows.

This page introduces Object-Oriented Programming (OOP), a programming approach that helps us structure programs more effectively by grouping related data and behaviour together. OOP is especially useful for modelling real-world systems and managing complex programs.

On this page, we will explore:

  1. What Object-Oriented Programming Is: Understanding the main idea behind OOP and how it differs from procedural programming.
  2. Classes and Objects: Learning how classes act as blueprints and objects represent real instances created from them.
  3. Why OOP Is Useful: Exploring the advantages of using OOP in large programs.

Let’s start by understanding the problem that OOP is designed to solve.

Programming Without Objects

In many early Python programs, data and behaviour are kept separate. For example, we might store values in variables and write functions that work on those values.

Example:

speed = 0
fuel = 50

def accelerate():
    global speed, fuel
    speed += 10
    fuel -= 5

In this example:

  • speed and fuel store data about a car.
  • The accelerate() function changes those values.

This approach works, but it has limitations.

The Problem with Larger Programs

Imagine extending this program so that it needs to handle:

  • Multiple cars
  • Different fuel levels
  • Different behaviours for each car

We would need:

  • Many variables (e.g. speed1, speed2, speed3)
  • Many functions to manage each set of variables

This makes the program:

  • Harder to read
  • Harder to maintain
  • More likely to contain errors

Object-Oriented Programming solves this problem by allowing us to bundle data and behaviour together.

What Is Object-Oriented Programming?

Object-Oriented Programming is a way of writing programs where data and the functions that operate on that data are grouped into a single structure called an object.

In OOP:

  • A class defines a blueprint for an object.
  • An object is a specific instance created from that class.

You can think of a class like a design for a car, and an object like an actual car built from that design.

Simple Example of a Class

Here is the most basic example of defining a class in Python:

class Car:
    pass

This class does not do anything yet, but it defines a new type called Car.

We can create objects from this class:

car1 = Car()
car2 = Car()

In this example:

  • Car is the class.
  • car1 and car2 are objects created from that class.

Each object is separate, even though they come from the same blueprint.

Why Use Object-Oriented Programming?

Object-Oriented Programming is useful because it allows us to:

  • Organise code more clearly
  • Model real-world systems more naturally
  • Reuse code efficiently
  • Manage large programs more easily

As programs grow, OOP helps keep code structured, readable, and maintainable — which is why it is widely used in professional software development.

In the next section, we’ll start using classes and objects directly and explore how they behave in practice.

Predict:

What do you think will happen?

Look at the following code. Before running it, try to predict what will be printed.

class Student:
    name = "Alex"

student1 = Student()
student2 = Student()

print(student1.name)
print(student2.name)

Consider these questions:

  • Will both objects print the same name?
  • Are student1 and student2 separate objects or the same?
  • What do you think a class is being used for here?

Hint:

The line class Student: defines a blueprint.
Each time Student() is used, a new object is created from that blueprint.

Run:

Let’s run the code

Now run the code below to see what actually happens when we create objects from a class.

What do you notice?

After running the code, think about the following:

  • Do both objects print the same value?
  • How many times is the class Student defined?
  • How many objects are created from it?

At this point, the class acts like a blueprint, and each object is created using that same blueprint.

Investigate:

Do objects affect each other?

Now let’s investigate what happens when we change the data stored in one object.
Use the editor below to experiment with the code.

Try this:

  • Add the line student1.name = "Jamie" before the print() statements.
  • Run the program again.

Questions to consider:

  • Did both objects change their name?
  • Or did only student1 change?

This investigation helps show that:

  • Objects are created from the same class
  • But each object can store its own values

Modify:

Fix the program

The following program creates a class and two objects, but it does not behave in the way we want.

Look at the code carefully and try to modify it so that each student can have a different name.

The problem

Right now:

  • Both students start with the same name.
  • The class defines a value that applies to every object.

Your task

Modify the code so that:

  • student1 has the name "Jamie"
  • student2 has the name "Taylor"

You should:

  • Create the objects first
  • Then assign different values to their name attributes

Hint:

Once an object has been created, you can change its attributes using dot notation:

object_name.attribute = value
Scroll to Top