Skip to content
Kuza Ndoto Academy

Classes & Objects

Beginner → IntermediateChapter 8 · Page 40 of 564 min readUpdated 15 Nov 2026

A class is a blueprint for creating objects, and an object is one thing built from that blueprint. Classes let you bundle data and the actions that use it, so your code can model real things such as students, bank accounts or matatu routes.

Why object-oriented programming?

Imagine tracking students with separate variables: name1, form1, name2, form2 and so on. It quickly becomes a mess. Object-oriented programming (OOP) groups each student's data and behaviour into one neat package. Python is fully object-oriented: even numbers and strings are objects.

print(type(5))
print(type("Kuza"))
print("kuza".upper())     # strings have methods because they are objects
<class 'int'> <class 'str'> KUZA

Defining a class and creating objects

Use the class keyword followed by a name in CapitalisedWords (called PascalCase). The simplest class has an empty body, written with pass.

class Phone:
    pass

my_phone = Phone()        # create (instantiate) an object
my_phone.brand = "Tecno"  # attach data to it
my_phone.storage_gb = 128

print(my_phone.brand, my_phone.storage_gb)
print(type(my_phone))
Tecno 128 <class '__main__.Phone'>

Calling the class like a function, Phone(), creates a new object. __main__ in the type name just means the class was defined in the file you ran.

A proper class: data plus behaviour

Real classes set up their data in a special method called __init__ and define methods: functions that belong to the class.

class Student:
    school = "Kuza High"                  # shared by every student

    def __init__(self, name, form):
        self.name = name                  # unique to each student
        self.form = form

    def introduce(self):
        return f"I am {self.name} in Form {self.form} at {self.school}"

    def promote(self):
        self.form += 1

neema = Student("Neema", 3)
juma = Student("Juma", 4)

print(neema.introduce())
print(juma.introduce())

neema.promote()
print(neema.form, juma.form)
I am Neema in Form 3 at Kuza High I am Juma in Form 4 at Kuza High 4 4

Here is what's happening:

  • Student("Neema", 3) creates a new object and automatically calls __init__ with name="Neema" and form=3.
  • self is the object being worked on. self.name = name stores the value on that object.
  • neema.introduce() calls the method; Python passes neema in as self for you.
  • Calling neema.promote() changed only Neema's form. Each object keeps its own data.
class Student school = "Kuza High" __init__(name, form) introduce() promote() Student("Neema", 3) Student("Juma", 4) neema (object) name = "Neema" form = 3 juma (object) name = "Juma" form = 4
Figure 1. One class, many objects. Each object stores its own attribute values (shown here just after creation), while the methods and the shared school attribute live on the class.

The details of __init__ and self have their own page: The __init__ Method.

Key vocabulary

TermMeaningIn the example
ClassThe blueprint or templateStudent
Object / instanceOne thing made from the classneema, juma
InstantiationCreating an object from a classStudent("Neema", 3)
AttributeA piece of data stored on an object or classname, form, school
MethodA function defined inside a classintroduce(), promote()

Checking types and identity

print(isinstance(neema, Student))
print(isinstance(neema, str))
print(neema is juma)          # two separate objects

twin = neema                   # another name for the SAME object
twin.name = "Neema W."
print(neema.name)
True False False Neema W.

Assigning an object to a new variable doesn't copy it. Both names point to the same object, so a change through one name shows up through the other, exactly like lists.

If you print an object without telling Python how, you get its class and memory address, which will differ on your computer:

print(juma)
<__main__.Student object at 0x000001F3A8C4B2D0>

You can make this friendlier with the __str__ method, covered in Dunder Methods.

Common mistake

Forgetting the brackets when creating an object: s = Student doesn't create a student; it just gives the class another name. Write s = Student("Amani", 2). Similarly, neema.introduce without brackets refers to the method instead of calling it.

When should you use a class?

Use a class when several pieces of data belong together and there are actions that work on that data, especially if you need many similar things. A bank account (owner, balance, deposit, withdraw), a quiz question (text, options, check answer) and a game character (health, position, move) are all good fits. For a small script that just transforms some numbers, plain functions are fine.

Key points

  • A class is a blueprint; an object (instance) is one thing built from it.
  • Create objects by calling the class: Student("Neema", 3).
  • Attributes hold data; methods are functions that act on the object through self.
  • Each object keeps its own data; assigning an object to another variable doesn't copy it.
Up next · Object-Oriented PythonThe __init__ MethodSet up new objects.
→

Tip: use the ← and → keys to move between pages.