6. Object-Oriented Programming (OOP)

Classes & Objects (OOP Explained Simply)

Understand Object-Oriented Programming without confusing jargon: learn how a class works like a blueprint, what __init__ and self actually mean, and when to use objects.

3 min readUpdated 2026-11-29
In This Lesson
  • Understand classes as blueprints and objects as independent instances
  • Initialize starting attributes with __init__ and track state using self
  • Bundle data and behavior together into clean object methods

Earlier we used dictionaries to represent things like a student: {"name": "Alex", "lessons_done": 3}

That works nicely—until you want each student to also carry actions (like "complete a lesson" or "print a progress badge"). Keeping data in one place and separate helper functions somewhere else can get messy in bigger apps.

A class lets you bundle data and actions together in one neat blueprint!

Think "Blueprint vs Real Thing"

1 Class Blueprint → Multiple Independent Objects

class Learner (Blueprint)

name, track, lessons_done

alex = Learner("Alex", "Python")

lessons_done = 2

maya = Learner("Maya", "SQL")

lessons_done = 1

Each object created from `Learner` keeps its own separate `name` and `lessons_done` values via `self`.

  • A class is an empty house blueprint drawn on paper. You cannot live inside a blueprint.
  • An object is a real house built from that blueprint. You can build 100 houses from 1 blueprint, and painting House #1 blue does not paint House #2 blue!

Writing Your First Class Slowly

python
class Learner:
    def __init__(self, name, track):
        self.name = name
        self.track = track
        self.lessons_done = 0

    def finish_lesson(self):
        self.lessons_done = self.lessons_done + 1
        print(f"{self.name} finished lesson #{self.lessons_done} in {self.track}!")

Let's demystify the two strange-looking words here:

  1. __init__ (double underscores on both sides): This special function runs automatically the moment you create a new Learner(...). Its job is to set the starting values (name, track, and lessons_done = 0).
  2. self: self simply means "THIS specific object." When you call alex.finish_lesson(), Python passes alex as self, so alex.lessons_done goes up without touching maya.lessons_done!

Building & Using Objects

python
alex = Learner("Alex", "Python")
maya = Learner("Maya", "SQL")

alex.finish_lesson()   # Alex finished lesson #1 in Python!
alex.finish_lesson()   # Alex finished lesson #2 in Python!
maya.finish_lesson()   # Maya finished lesson #1 in SQL!

print(alex.lessons_done)  # 2
print(maya.lessons_done)  # 1

Look how clean and readable alex.finish_lesson() is—just like a built-in method such as my_list.append(...)! (In fact, lists and strings in Python are built using classes too!)

Forgetting self Inside Class Functions

Every function inside a class needs self as its very first parameter (def finish_lesson(self):). If you forget self, calling alex.finish_lesson() will crash with TypeError.

Try It Yourself in the Playground

Try adding a method add_bonus(self, count) or creating a third learner:

Python 3Try It Live: BankAccount Class
Output

Click “Run Code” to execute this snippet right in your browser.

Mini-Challenge: Add a show_balance(self) method to BankAccount that prints "{owner}'s current balance is ${balance}" and call it on both accounts.

Quick Checkpoints

Check Your Understanding

When does Python run __init__ automatically?

Check Your Understanding

In def deposit(self, amount):, what does self represent?