KevsRobots Learning Platform
18% Percent Complete
By Kevin McAleer, 5 Minutes
In this lesson, we will explore the concepts of classes and objects in MicroPython. Understanding these concepts is fundamental to mastering object-oriented programming (OOP) and will help you build more organized and reusable code.
A class
is a blueprint for creating objects
. It defines the properties
and methods
that an object will have. In MicroPython, you can define a class using the class
keyword.
An instance
of a class is also known as an object
. By instance, we mean a single occurrence of an object:
a = 1 # a is an instance of an integer object
b = "hello" # b is an instance of a string object
c = [1, 2, 3] # c is an instance of a list object
A class property
is the name we give to variables within a class. A class method
is the name we give to functions within a class.
class Robot:
robot_name = "" # this is a class property
color = "" # this is another class property
def say_hello(self):
# This is a class method
print(f"Hello, I am {self.robot_name}")
def change_color(self, new_color):
# This is another class method
self.color = new_color
We use classes to model real-world things, such as a Robot or a Car. For example, we can create a class called Robot
with properties like robot_name
and color
, and methods like say_hello
and change_color
βsimilar to a real-world robot.
class Robot:
robot_name = ""
def __init__(self, name, color):
self.robot_name = name
self.color = color
def say_hello(self):
print(f"Hello, I am {self.robot_name}")
def change_color(self, new_color):
self.color = new_color
You can create multiple objects from the same class, each with its own set of properties and methods. Note that property values can differ for each object, but the methods remain the same.
r2d2 = Robot("R2D2", "Blue")
c3po = Robot("C3PO", "Gold")
Constructors
Notice the
__init__(self, name, color)
method in theRobot
example above. This is called theconstructor
of the class. It is called when a new object is created from the class.We use the constructor to set the initial values of the objectβs properties.
The convention is to name classes with an uppercase letter at the start of each word. In the example above, we have a class called Robot
.
Notice how the methods within the class are indented. This means these methods are only accessible within the class or via the dot β.
β operator.
Also, notice that there is a variable called robot_name
within the class. This is a class variable
shared by all instances of the class.
In Python and MicroPython, an object
is an instance of a class. It is created using the class as a blueprint.
An object is like a variable that also has functions attached to it. These functions are the class methods
.
You can call the methods of an object using the dot β.
β operator.
# Create an object of the Robot class
r1 = Robot("R2D2", "Blue")
# Call the say_hello method
r1.say_hello()
# Change the color of the robot
r1.change_color("Red")
# Access the color of the robot
print(r1.color)
Almost everything in Python is an object, including int
, float
, list
, dict
, and str
. This means they have properties and methods that you can access.
You may have already used objects and their methods without realizing it. For example, the str
object has a method called upper()
that converts the string to uppercase.
message = "hello" # create a string called message and store the value "hello"
print(message.upper()) # prints out the message string in uppercase, this uses the str object's upper() method
In the __init__
method and other class methods, you will see a parameter called self
. This is a reference to the current instance of the class. It is used to access the variables and methods of the class.
self
is always the first parameter in a class method; it cannot be omitted.
class Robot:
# Class Properties
robot_name = "" # this is a class property
color = "" # this is another class property
def __init__(self, name, color):
self.robot_name = name # changes this object's robot_name to the name provided
self.color = color # changes this object's color to the color provided
def change_color(self, new_color):
# This is a class method
self.color = new_color
Did You Know?
β
self
β can actually have any name, but it is a convention to use βself
β in Python.You could use β
this
β or βme
β or any other name, but it is best to stick with βself
β to avoid confusion. As long as the first parameter of a class method is the reference to the current instance of the class, it will work.
In this lesson, weβve learned about classes and objects in MicroPython. We explored how classes act as blueprints for creating objects, how to define properties and methods, and the importance of the self
parameter. We also covered naming conventions and the concept that everything in Python is an object.
You can use the arrows β β
on your keyboard to navigate between lessons.