By Kevin McAleer, 5 Minutes
Page last updated November 24, 2026

By the end of this lesson you will be able to define a class, create objects from it, add methods, and use inheritance to build a new class from an existing one.
Once a robot has a name, a battery level, some sensors and a handful of things it can do, you end up with lots of related variables and functions floating around. Object-oriented programming (OOP) bundles the data and the behaviour together into one neat package.
A class is a blueprint. An object (also called an instance) is a thing built from that blueprint. Every robot built from a Robot blueprint has its own name and battery.
classDiagram
class Robot {
name
battery
drive()
status()
}
class FlyingRobot {
fly()
}
Robot <|-- FlyingRobot
The special __init__ method runs when you create an object and sets up its attributes. self is the object being built.
chip and bolt are two separate objects. Changing one never changes the other.
Methods are functions defined inside a class. They do things with the object’s own data, which they reach through self.
Why this matters: the rule “driving costs 10 battery, and you cannot drive when it is low” lives in one place, with the data it protects.
Inheritance lets you make a new class from an existing one, adding or changing only what is different. The new class is the child (or derived) class and the one it inherits from is the parent (or base) class.
super() gives you the parent class, so FlyingRobot can reuse Robot’s setup without copying it.
Robot class with a name and a battery level, and a drive() method that reduces the battery by 10 and prints a message. Make two robots and drive one of them.charge() method that sets the battery back to 100.FlyingRobot that inherits from Robot and adds a fly() method.status() method to Robot that returns a string like "Chip: 80% battery", then check that FlyingRobot can use it too.TypeError: Robot.drive() takes 1 positional argument but 2 were given.self as its first parameter, and you call it without passing self yourself: chip.drive().Why: Python passes the object in automatically as the first argument.
AttributeError: 'Robot' object has no attribute 'battery'.self.battery = battery in __init__, and use self.battery (not just battery) inside methods.Why: Plain variable names inside a method vanish when it ends. Attributes on self stay with the object.
name.super().__init__(...) inside the child’s __init__.__init__, the parent’s one does not run unless you call it.Quick quiz 1 of 3
What does self refer to inside a method?
self is the instance, so self.battery is that object’s own battery.Quick quiz 2 of 3
What does super().__init__(name, battery) do in the FlyingRobot class?
super() gives you access to the parent class, so FlyingRobot can reuse Robot’s setup.Quick quiz 3 of 3
What is the difference between a class and an object?
Robot is the class; chip and bolt are objects made from it.You can now model a robot with a class, give it methods and extend it with inheritance. This finishes the core language. From the next lesson we go on a tour of what Python can do in the real world, starting with data.
Comments