Object-Oriented Programming
Until now, we've been writing a list of instructions for Python to follow from top to bottom. That works great for small programs. But when programs grow huge, we need a better way to organise things. Object-Oriented Programming (OOP) is a completely different way of thinking about code — by modelling the real world as objects that have properties and can do things.
What is a Class?
A ClassA blueprint or template that defines the properties and behaviours that all objects of that type will have. is a blueprint. It's not the real thing — it's the design of the real thing. You define a class once, then create as many individual objects from it as you want.
Classes are created with the class keyword. Inside a class, you define a special function called __init__
(that's two underscores on each side). This is the constructor — it runs automatically every time you create a new object from the class.
class Dog: def __init__(self, name, breed): self.name = name self.breed = breed
Dog. Note: class names are written in CapitalCase.self refers to the specific dog being created — it's how Python tracks which dog is which.Creating objects from a class
An ObjectA specific instance created from a class. Each object has its own values for the properties defined in the class blueprint. is what you actually build from the blueprint. You can build as many objects as you like from the same class, and each one gets its own data.
# Create two different Dog objects from the same blueprint dog1 = Dog("Buddy", "Labrador") dog2 = Dog("Max", "Poodle") print(dog1.name) # Buddy print(dog2.breed) # Poodle
Dog class is the blueprint. dog1 and dog2 are the actual dogs. They share the same blueprint but each has their own name and breed.
Functions that belong to a class
A MethodA function defined inside a class. Methods describe what objects of that class can do.
is just a function that lives inside a class. It defines what an object can do.
Every method's first parameter must always be self — Python uses this to know which object called the method.
class Dog: def __init__(self, name, breed): self.name = name self.breed = breed def bark(self): print(self.name + " says: Woof!") def describe(self): print(self.name, "is a", self.breed) # Create and use dog1 = Dog("Buddy", "Labrador") dog1.bark() dog1.describe()
bark. self is always the first parameter — but you don't pass it yourself. Python handles that!dog1 as self behind the scenes.
Think about a Cookie Cutter and Cookies.
The Class (Cookie Cutter): A star-shaped cookie cutter. It's not a cookie itself, but it defines exactly what every star cookie will look like.
Objects (The Cookies): Each cookie you press out using that cutter. They all have the same star shape, but you can decorate each one differently (chocolate chips vs sprinkles — different property values!).
Methods (What Cookies Can Do): Be eaten. Be photographed. Be sold. These are the actions available to every cookie object.
Class → Objects
The Snake Class
class Snake:
def __init__(self, name):
self.name = namedef introduce(self):
print(self.name +
" the great!")With great classes comes great responsibility.
Your First Class
Create, modify, and extend the Dog class. Then build your own class from scratch!
- Create a third dog called
dog3with your favourite dog name and breed, then call.bark()on it. - Add a new method called
sit(self)that prints"[name] sits down." - Advanced: Create a completely new class called
Carwith propertiesbrandandspeed, and a method calleddrive().
Going Deeper (OOP) — Done! 🏗️
- A Class is a blueprint. Use the
classkeyword to define one. - An Object is a specific instance built from a class. One blueprint, infinite objects.
- __init__ is the constructor — it runs automatically when you create a new object.
- self is how each object refers to itself inside the class.
- Properties (like
self.name) store an object's data. - Methods are functions inside a class that define what an object can do.