Tutorials › Python Tutorial › Python Classes and Objects

Python Classes and Objects

13 min read · Lesson 19 of 24

Python is an object-oriented language. Every value you have used – strings, lists, numbers – is an object, and every object is built from a class. A class is a blueprint; an object is a thing made from that blueprint. In this lesson you will write your own classes.

Why classes?

Imagine a SACCO app that tracks member accounts. Each account has data (owner, balance) and actions (deposit, withdraw). A class bundles that data and those actions together, so every account looks after itself.

Creating a class and an object

Use the class keyword. By convention class names use CapitalisedWords.

Example PYTHON
class Account:
    pass

acc = Account()
print(type(acc).__name__)
Result
Account
Try it Yourself »

Account() creates a new object (also called an instance) of the class.

The __init__ method

__init__ is a special method that runs automatically when an object is created. Use it to set up the object's data, which are called attributes.

Example PYTHON
class Account:
    def __init__(self, owner, balance=0):
        self.owner = owner
        self.balance = balance

a = Account("Wambui", 5000)
b = Account("Odera")
print(a.owner, a.balance)
print(b.owner, b.balance)
Result
Wambui 5000
Odera 0
Try it Yourself »

self refers to the object being created or used. self.owner = owner stores the argument on that particular object. Each object has its own copy of the attributes.

Note: self must be the first parameter of every method, but you never pass it yourself. Python fills it in: a.deposit(100) becomes Account.deposit(a, 100) behind the scenes.

Methods

A method is a function defined inside a class. It can read and change the object's attributes through self.

Example PYTHON
class Account:
    def __init__(self, owner, balance=0):
        self.owner = owner
        self.balance = balance

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

    def withdraw(self, amount):
        if amount > self.balance:
            print(f"Sorry {self.owner}, insufficient funds.")
            return False
        self.balance -= amount
        return True

acc = Account("Njoroge", 1000)
acc.deposit(500)
acc.withdraw(2000)
acc.withdraw(300)
print("Balance:", acc.balance)
Result
Sorry Njoroge, insufficient funds.
Balance: 1200
Try it Yourself »

The __str__ method

Printing an object normally shows something unhelpful like <__main__.Account object at 0x...>. Define __str__ to control what print() shows.

Example PYTHON
class Product:
    def __init__(self, name, price):
        self.name = name
        self.price = price

    def __str__(self):
        return f"{self.name} - KES {self.price:,}"

item = Product("Solar lamp", 2450)
print(item)
Result
Solar lamp - KES 2,450
Try it Yourself »

Class attributes

Attributes set directly in the class body (not inside __init__) are shared by all objects of that class.

Example PYTHON
class Student:
    school = "Kakamega School"     # class attribute

    def __init__(self, name):
        self.name = name           # instance attribute

s1 = Student("Shikuku")
s2 = Student("Makokha")
print(s1.name, "-", s1.school)
print(s2.name, "-", s2.school)
Result
Shikuku - Kakamega School
Makokha - Kakamega School
Try it Yourself »

Changing attributes

You can change an object's attributes directly, though it is often cleaner to do it through methods.

Example PYTHON
class Phone:
    def __init__(self, model, battery):
        self.model = model
        self.battery = battery

p = Phone("Tecno Spark", 80)
p.battery = 45
print(p.model, p.battery)
Result
Tecno Spark 45
Try it Yourself »

Inheritance

A class can inherit from another class. The child class gets all the parent's attributes and methods, and can add new ones or change existing ones.

Example PYTHON
class Vehicle:
    def __init__(self, plate, seats):
        self.plate = plate
        self.seats = seats

    def describe(self):
        return f"{self.plate} with {self.seats} seats"

class Matatu(Vehicle):
    def __init__(self, plate, seats, route):
        super().__init__(plate, seats)
        self.route = route

    def describe(self):
        return super().describe() + f" on route {self.route}"

car = Vehicle("KDA 123A", 5)
mat = Matatu("KCB 456B", 14, "Kisumu - Kakamega")
print(car.describe())
print(mat.describe())
print(isinstance(mat, Vehicle))
Result
KDA 123A with 5 seats
KCB 456B with 14 seats on route Kisumu - Kakamega
True
Try it Yourself »

super() gives access to the parent class, so the child can reuse the parent's __init__ and describe() instead of repeating that code. A Matatu is also a Vehicle, which is why isinstance() returns True.

Tip: Use inheritance when the child truly "is a" kind of the parent (a matatu is a vehicle). If one thing merely has another, store it as an attribute instead.

Exercises

Exercise

Use the keyword that creates a class.

 Farm:
    pass
Exercise

Complete the name of the method that runs when an object is created.

def (self, name):
    self.name = name

Next: Python Modules.

Sign in or create a free account to track progress, earn XP, badges and a certificate.