Lesson 8 · Introduction to Classes
Lesson 8 · Introduction to Classes
Teacher mode is on. Toggle it off (bottom-right) to preview the student view.
Your line-following programs work — but the sensor logic, the driving, and the
control math are all tangled together. In this lesson you'll build your first
class, LineSensor, that bundles all the sensor logic into one clean, reusable
object. It's a big new idea; we'll take it a step at a time.
Learning Objectives
By the end of this lesson you will be able to:
- Explain why a class helps organize related data and actions
- Define a class with
class, and initialize it with__init__ - Explain what
selfmeans and why methods take it - Write and call methods on an object you create
What a class is
A class is a blueprint that groups related data and actions together. Think of a TV remote: its data is the current channel and volume; its actions are change-channel and volume-up. The class describes what every remote is like; an actual remote you hold is an object built from that blueprint.
You've used classes all along — DifferentialDrive, Reflectance, and Board are
all classes. Now you'll write your own. The goal is code that reads like English:
sensor = LineSensor()
error = sensor.get_error()
if sensor.is_at_cross():
print("Cross!")
That last line is far clearer than left > 0.5 and right > 0.5 scattered through
your program — and you can reuse the whole thing in any program.
What's the difference between a class and an object?
__init__ and self
Start the class and give it a constructor — the special __init__ method Python
runs automatically whenever you create an object:
from XRPLib.reflectance import Reflectance
class LineSensor:
def __init__(self):
self.reflectance = Reflectance.get_default_reflectance()
self.threshold = 0.5
The pieces:
class LineSensor:— defines a new type (capitalized by convention).def __init__(self):— the constructor, run automatically onLineSensor().self— the object itself, "me."self.threshold = 0.5means "this object's threshold is 0.5," and it sticks around as long as the object does.
sensor = LineSensor() # __init__ runs now
print(sensor.threshold) # 0.5
When does the __init__ method run?
Methods
A method is just a function that lives inside a class and takes self as its
first parameter — that's how it reaches the object's own data. Build up LineSensor
with the sensor logic from earlier lessons:
class LineSensor:
def __init__(self):
self.reflectance = Reflectance.get_default_reflectance()
self.threshold = 0.5
def get_left(self):
return self.reflectance.get_left()
def get_right(self):
return self.reflectance.get_right()
def get_error(self):
return self.get_left() - self.get_right()
def is_at_cross(self):
return self.get_left() > self.threshold and self.get_right() > self.threshold
def is_off_line(self):
return self.get_left() < self.threshold and self.get_right() < self.threshold
Notice a method can call other methods on the same object through self (like
get_error() using self.get_left()). And when you call sensor.get_error(),
Python passes the object in as self automatically — you never type it yourself.
sensor = LineSensor()
print(sensor.get_error()) # left − right
print(sensor.is_at_cross()) # True / False
You have `sensor = LineSensor()`. How do you call its get_error method?
Write is_on_line() to return True when AT LEAST ONE sensor is on the line. Which body is correct?
Before and after
See what the class buys you. The old tangled way:
reflectance = Reflectance.get_default_reflectance()
threshold = 0.5
left = reflectance.get_left()
right = reflectance.get_right()
error = left - right
at_cross = left > threshold and right > threshold
The class way:
sensor = LineSensor()
error = sensor.get_error()
at_cross = sensor.is_at_cross()
Same behavior, but the second reads like plain language and can be dropped into any program. Test yours with a loop that prints the error while you slide the robot over the line by hand.
Real-world connections
Classes are how large software stays organized:
Everything you import
Every library you use — the robot's own — is built from classes with methods you call.
Game objects
A player, an enemy, a bullet — each is a class with its own data and actions.
Real systems
A User, an Order, a Message — big apps are thousands of cooperating classes.
Wrap-up
- What keyword defines a class, and which method runs automatically? (
class;__init__.) - What does
selfrefer to? (The object itself — "this one.") - How is a method different from a plain function? (It lives in a class and takes
self.)