Skip to content
BetaDE
Lesson 24 of 35Python

Python · Lesson 24 · 35 minutes

Classes and objects

What you'll learn

  • Write your own blueprint for objects with class
  • Set attributes in __init__ and use them through self
  • Write methods that change an object

Blueprint and building

Imagine you build robots. Every robot has a name and a battery level, and every one can drive. Instead of creating separate variables for each robot, you write a blueprint: a class. From the blueprint you then build as many objects as you like.

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

bello = Dog("Bello", 3)
luna = Dog("Luna", 7)

print(bello.name)
print(luna.age)

Class names start with a capital letter (Dog, Robot). Press Steps: in memory you’ll see two objects with their own values.

init and self

__init__ (two underscores before and after) runs automatically when a new object is built. This is where you set the attributes.

self means “this object here”. self.name = name means: store the name in this object. That’s why every dog can have its own name.

Methods: what an object can do

Functions inside a class are called methods. They always get self as their first parameter, so they can read and change the object’s attributes:

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

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

    def show(self):
        return f"{self.owner} has {self.balance} €"

my_account = Account("Mia")
my_account.deposit(20)
my_account.deposit(15)
print(my_account.show())

When you call my_account.deposit(20) you don’t pass self – Python automatically inserts the object in front of the dot.

Remember: A class is the blueprint, an object is the built thing. Dog is a class, bello is an object.

Why this matters for robots

When you program LEGO SPIKE later, you work with objects all the time: a motor is an object with methods like run_angle(), a sensor one with distance(). Now you know what that looks like from the inside.

Quick check

Before you start the task: what does this program print? Type your guess – then Python works it out.

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

    def learn(self):
        self.tricks += 1


rex = Dog("Rex")
rex.learn()
rex.learn()
print(rex.name, rex.tricks)

Your task

Your turn

Complete the class Robot. A new robot has a name and starts with battery = 100. The method drive(metres) uses 1 % battery per 10 metres (so metres // 10), but the battery must never drop below 0. The method status() returns a text like Robo: 95 % battery.

class Robot:
    def __init__(self, name):
        self.name = name
        # create the battery here

    def drive(self, metres):
        # use up battery
        pass

    def status(self):
        # return the status text
        pass


robo = Robot("Robo")
robo.drive(50)
print(robo.status())

Solve it without hints to earn a ⭐ on your progress page.

Show solution

Try it yourself first – mistakes teach you the most. Here is one possible solution:

class Robot:
    def __init__(self, name):
        self.name = name
        self.battery = 100

    def drive(self, metres):
        self.battery = self.battery - metres // 10
        if self.battery < 0:
            self.battery = 0

    def status(self):
        return f"{self.name}: {self.battery} % battery"


robo = Robot("Robo")
robo.drive(50)
print(robo.status())
Practise more in the training roomEndless new tasks on this topic – with XP and streaks.