Build Your Own AI Assistant Part 1 - Creating the Assistant
116820 Views
Is the new Raspberry Pi AI Kit better than Google Coral?
114678 Views
Control Arduino with Python using Firmata / PyFirmata
87081 Views
How to Map with LiDAR - using a Raspberry Pi Zero 2W, RPLidar and Rviz
57314 Views
Creating a Supercomputer with a Raspberry Pi 5 Cluster and Docker Swarm!
53588 Views
Node-Red Automation, MQTT, NodeMCU & MicroPython
52067 Views
LidarBot
Snaszy NAS a 3D printed NAS for Raspberry Pi
Waveshare CM5 boards
The Best Arduino Robot for Beginners
SMARS Lab upgrade with PyCharm
Chicken Nugget Piano
Mini-Rack 3D Design Tutorial
0h 20m
Using the Raspberry Pi Pico's Built-in Temperature Sensor
0h 24m
Getting Started with SQL
0h 32m
Introduction to the Linux Command Line on Raspberry Pi OS
0h 42m
How to install MicroPython
0h 8m
Wall Drawing Robot Tutorial
0h 22m
Learn Linux from the basics to advanced topics.
Learn how to use a Raspberry Pi Pico
Learn MicroPython the best language for MicroControllers
Learn Docker, the leading containerization platform. Docker is used to build, ship, and run applications in a consistent and reliable manner, making it a popular choice for DevOps and cloud-native development.
Learn how to build SMARS robots, starting with the 3D Printing the model, Designing SMARS and Programming SMARS
Learn how to build robots, starting with the basics, then move on to learning Python and MicroPython for microcontrollers, finally learn how to make things with Fusion 360.
Learn Python, the most popular programming language in the world. Python is used in many different areas, including Web Development, Data Science, Machine Learning, Robotics and more.
Learn how to create robots in 3D, using Fusion 360 and FreeCAD. The models can be printed out using a 3d printer and then assembled into a physical robot.
Learn how to create Databases in Python, with SQLite3 and Redis.
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.
class
objects
properties
methods
An instance of a class is also known as an object. By instance, we mean a single occurrence of an object:
instance
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.
property
method
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.
Robot
robot_name
color
say_hello
change_color
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 the Robot example above. This is called the constructor 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.
Notice the __init__(self, name, color) method in the Robot example above. This is called the constructor of the class. It is called when a new object is created from the class.
__init__(self, name, color)
constructor
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.
class variable
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.
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.
int
float
list
dict
str
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.
upper()
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.
__init__
self
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.
“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.
this
me
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.
< Previous Next >