Stage 10 Going Deeper
Lesson 10 of 12 +10 XP

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.

Sarpanch
The real world is made of "things" — dogs, cars, bank accounts. OOP lets us describe those things in code and create as many of them as we need! 🌍

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.

dog.py
class Dog:
    def __init__(self, name, breed):
        self.name = name
        self.breed = breed
1class Dog:This creates a blueprint called Dog. Note: class names are written in CapitalCase.
2def __init__(self, name, breed):This special function runs when you create a new Dog. self refers to the specific dog being created — it's how Python tracks which dog is which.
3self.name = nameStore the name value as a property of this particular dog object.

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
💡 Key idea: The 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.

dog_with_methods.py
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()
6def bark(self):A method named bark. self is always the first parameter — but you don't pass it yourself. Python handles that!
14dog1.bark()Call the method using dot notation. Python automatically sends 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

class Dog: name breed bark() describe() 📋 Blueprint dog1 = Dog(...) name = "Buddy" breed = "Labrador" 🐕 Object 1 dog2 = Dog(...) name = "Max" breed = "Poodle" 🐩 Object 2
One class blueprint creates unlimited unique objects — each with its own property values.

The Snake Class

Panel 1
Sarpanch
"I'll create a Snake class to describe myself!"

class Snake:
  def __init__(self, name):
    self.name = name
Panel 2
Sarpanch
"And I'll add a method!"

def introduce(self):
  print(self.name +
  " the great!")
Panel 3
Sarpanch
"Now I can create a thousand Sarpanch clones from one blueprint. Mwahaha."

With great classes comes great responsibility.

Your First Class

Create, modify, and extend the Dog class. Then build your own class from scratch!

💡 Challenges:
  1. Create a third dog called dog3 with your favourite dog name and breed, then call .bark() on it.
  2. Add a new method called sit(self) that prints "[name] sits down."
  3. Advanced: Create a completely new class called Car with properties brand and speed, and a method called drive().

Going Deeper (OOP) — Done! 🏗️

  • A Class is a blueprint. Use the class keyword 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.
Sarpanch victorious
OOP is how almost all professional software is built! You've just learned one of the most important concepts in all of programming. One more stage before we build real projects! 🚀