๐Ÿ
๐Ÿ Intermediate Python ยท PY-201 ยท Session 4 of 6

Objects & Classes: Your Own Types

Classes let you bundle data and the behaviour that goes with it into your own types โ€” a way of thinking that models the world and keeps big programs organised.

โฑ 9 min read๐Ÿ“… Updated 2026-07-28๐Ÿ“Š Tables, charts & examples

You've used objects all along - strings, lists and files are objects, each with data and methods (like list.append()). Now you'll make your own. A class bundles related data together with the functions that work on it, letting you model a thing - a bank account, a player, a shopping cart - as a single, self-contained unit. It's a powerful way to keep larger programs organised.

๐Ÿ“Œ Worked example
From tangle to tidy. A small game tracked three players with variables like p1_health, p2_health, p3_health and matching position and score variables - fifteen loose variables that were constantly getting mixed up. Rewritten with a Player class, each player became one object holding its own health, position and score, with move() and take_damage() methods acting on self. Fifteen tangled variables became three tidy objects, mix-ups vanished, and adding a fourth player was one line. That's what classes are for: keeping each thing's state and behaviour together.

A class bundles data + behaviour

A class is a blueprint; an object is one made from it. The __init__ method sets up each object's data; other methods are its behaviour.

class BankAccount:
    def __init__(self, owner):
        self.owner = owner       # data (attributes)
        self.balance = 0

    def deposit(self, amount):   # behaviour (methods)
        self.balance += amount

    def withdraw(self, amount):
        if amount > self.balance:
            raise ValueError("insufficient funds")
        self.balance -= amount

acc = BankAccount("Ada")     # make an object
acc.deposit(100)
acc.balance                  # 100
๐ŸŽฏ
self is just 'this particular object'. Every method's first parameter is self - it's how a method refers to the specific object it was called on, so acc.deposit(100) updates acc's balance, not some other account's. You never pass self yourself; Python fills it in. Once that clicks, classes stop being mysterious: self.balance just means 'this object's balance', and methods are functions that live on the object and act on its data.

Why classes help

Classes shine when data and the operations on it belong together, and when you have many of the same kind of thing.

Without classesWith a class
balance, owner as loose variablesOne account object holding both
Functions taking balance aroundMethods that act on their own data
Easy to mix up whose is whoseEach object keeps its own state
Hard to have many accountsMake as many objects as you like

The class keeps each account's data and rules together and safe (withdraw can enforce 'no overdraft'). This bundling - state plus the behaviour that guards and uses it - is what keeps big programs from turning into a tangle of loose variables.

Model things as objects

  • A class is a blueprint; an object is one built from it.
  • __init__ sets up each object's data; other methods are its behaviour.
  • self means 'this particular object' - Python passes it for you.
  • Use classes when data and its operations belong together, or you need many of a thing.

๐Ÿงฉ Do it with LarzOS

Turn this lesson into action with the exact free tools and AI workflows for the job:

๐ŸŽฏ Key takeaways

  • A class bundles data (attributes) with the behaviour (methods) that works on it.
  • A class is a blueprint; an object is a specific instance made from it via __init__.
  • 'self' just means 'this particular object' - Python passes it automatically.
  • Classes keep each thing's state and rules together, which keeps big programs organised.

Frequently asked

What exactly is 'self' and why is it everywhere?

self is how an object refers to itself from inside its own methods. When you write acc.deposit(100), Python calls the deposit method and automatically passes acc as the first argument, which the method receives as self - so inside deposit, self IS acc, and self.balance means acc's balance specifically. This is what lets one class serve many objects: if you have three bank accounts, each method call knows which account to act on because self is filled in with the particular object you called it on. You always list self as the first parameter when defining a method, but you never pass it yourself when calling - Python does that. Once you see self as simply 'the object this was called on', the whole thing demystifies: attributes are self.something, and methods are just functions that operate on their own object's data via self.

When should I actually use a class instead of just functions and variables?

Reach for a class when you have data and behaviour that naturally belong together, especially when you'll have many of the same kind of thing, each with its own state. A bank account (balance + deposit/withdraw), a game player (health, position + move/attack), a shopping cart (items + add/total) - all are natural classes, because bundling the data with the operations that guard and use it keeps things tidy and prevents mix-ups. If you're just doing a quick calculation or a one-off transformation, plain functions are simpler and better - don't force everything into classes. A good signal is when you find yourself passing the same group of variables into many functions, or tracking several instances of the same concept with parallel variables: that's a class waiting to happen. Use classes to organise; don't use them ceremonially where a function would do.

Session 4 of 6 complete

Next up: Cleaner Code: Pythonic Idioms

Continue to Session 5 โ†’

Related lessons

More in this course

Course home → · Browse all courses →