Know if you're actually ready. Take the Python quiz → get your AI readiness report.Take the free test →

Study guide · Python

Python Classes and OOP: What Every Exam Tests

Object-oriented programming is a major exam topic. Here's what you actually need to know — from __init__ to inheritance to dunder methods.

Defining a class and __init__

__init__ is the initializer (not strictly a constructor). It runs when an instance is created. self refers to the instance being created.

class Dog:
    def __init__(self, name, breed):
        self.name = name
        self.breed = breed

    def bark(self):
        return f"{self.name} says woof!"

rex = Dog("Rex", "Labrador")
print(rex.bark())   # "Rex says woof!"
Noteself is a convention, not a keyword. You could name it anything — but don't. Exams sometimes use this as a trick question.

Instance variables vs class variables

Instance variables belong to each object. Class variables are shared across all instances.

class Counter:
    count = 0  # class variable

    def __init__(self):
        Counter.count += 1
        self.id = Counter.count  # instance variable

a = Counter()
b = Counter()
print(Counter.count)  # 2
print(a.id)           # 1
print(b.id)           # 2
NoteModifying a class variable via an instance creates a new instance variable that shadows it. This is a classic exam trap.

Inheritance

A child class inherits all methods from its parent. It can override methods and call the parent with super().

class Animal:
    def __init__(self, name):
        self.name = name

    def speak(self):
        return "..."

class Cat(Animal):
    def speak(self):
        return f"{self.name} says meow"

class Dog(Animal):
    def speak(self):
        return f"{self.name} says woof"

class GoldenRetriever(Dog):
    def __init__(self, name):
        super().__init__(name)
        self.friendly = True

Static methods and class methods

Regular methods receive self. Class methods receive the class via cls. Static methods receive neither.

class MathUtils:
    pi = 3.14159

    @classmethod
    def circle_info(cls):
        return f"pi = {cls.pi}"

    @staticmethod
    def add(a, b):
        return a + b

print(MathUtils.add(2, 3))       # 5
print(MathUtils.circle_info())   # pi = 3.14159

Key dunder (magic) methods

Dunder methods let you define how objects behave with operators and built-in functions.

class Vector:
    def __init__(self, x, y):
        self.x = x
        self.y = y

    def __str__(self):
        return f"({self.x}, {self.y})"

    def __repr__(self):
        return f"Vector({self.x}, {self.y})"

    def __add__(self, other):
        return Vector(self.x + other.x, self.y + other.y)

    def __len__(self):
        return 2

v1, v2 = Vector(1, 2), Vector(3, 4)
print(v1 + v2)   # (4, 6)
Note__str__ is for human-readable output. __repr__ is for unambiguous representation. If only __repr__ is defined, it's used for both.
Exam tipThe most common OOP exam mistake is forgetting self in method definitions. Also know when to use super().__init__() — without it, the parent class doesn't initialise properly.

Want a person to help?

Struggling with Python? Work 1:1 with a vetted Python tutor on Wyzant. (affiliate link)

Find a Python tutor →
← All Python guides