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
24% Percent Complete
By Kevin McAleer, 3 Minutes
In OOP, Abstraction means hiding the complexity of a system by exposing only the necessary details.
Abstraction
We achieve this by creating simple interfaces that allow interaction with the code without needing to know its inner workings.
This approach allows us to change how the system works without affecting the code that interacts with it, or the user noticing any change.
In MicroPython, we use classes to create abstractions. A class is a blueprint for creating objects that represent real-world entities.
classes
In the example below, we model a popular road bike, the Yamaha MT-07, using a class. We provide two public methods ride and get_mileage to interact with the bike object.
ride
get_mileage
The Bike class also has a private property __mileage that stores the mileage of the bike. __mileage can only be accessed by the ride() method to increase the mileage, and get_mileage() method to return the current mileage the bike has traveled.
__mileage
ride()
get_mileage()
class Bike: def __init__(self, make, model): self.make = make self.model = model self.__mileage = 0 def ride(self, distance): self.__mileage += distance def get_mileage(self): return self.__mileage my_bike = Bike("Yamaha", "MT-07") my_bike.ride(100) print(my_bike.get_mileage())
The Bike class is an abstraction of a real-world bike. It hides the complexity of how the bike works and provides a simple interface for interacting with the bike object.
MicroPython allows us to create modular code by using classes to create objects that represent real-world entities.
Classes can be composed of other classes, which allows us to create complex objects that are made up of simpler objects.
For example, if we want to create a robot, we could create a class for the robot and then create objects for the sensors, motors, and other components that make up the robot.
class Sensor: def __init__(self, type): self.type = type def read_value(self): # Simulate reading a sensor value return 42 class Motor: def __init__(self, power): self.power = power def move(self, direction): print(f"Moving {direction} with power {self.power}") class Robot: def __init__(self, name): self.name = name self.sensor = Sensor("Ultrasonic") self.motor = Motor(100) def move_forward(self): self.motor.move("forward") def read_sensor(self): return self.sensor.read_value() robot = Robot("Robo") robot.move_forward() print(robot.read_sensor())
We cover Modules in a future lesson.
Modules
Abstraction is about hiding complexity by exposing only the necessary details, while Encapsulation is about hiding properties and methods using access modifiers. We cover Encapsulation in a future lesson.
Encapsulation
Think of a car. You don’t need to know how the engine works to drive it. You just use the pedals, steering wheel, and gear stick. The engine is abstracted away from you.
Abstraction in OOP allows us to hide complexity and create simple interfaces for interacting with systems. It helps simplify, add flexibility, and reuse code in our programs.
< Previous Next >