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
30% Percent Complete
By Kevin McAleer, 4 Minutes
In object-oriented programming (OOP), Inheritance is a way to create new classes based on existing classes. The new classes, known as derived classes, inherit attributes and methods from the existing classes, known as base classes.
Inheritance
derived classes
base classes
class Animal: def __init__(self, name): self.name = name def speak(self): print("Animal speaking") class Dog(Animal): def __init__(self, name, breed): super().__init__(name) self.breed = breed def speak(self): print("Dog barking") animal = Animal("Animal") dog = Dog("Dog", "Labrador") dog.speak()
First we define the base class, Animal, it has a name property and a speak method. Next we define a Dog class, that inherits from the Animal class.
Animal
name
speak
Dog
Notice that the Dog class has a parameter - the Base class it inherits from.
Also notice the use of super() to call the base class constructor; we do this to initialize the base class properties in the derived class.
super()
constructor
Inheritance is a key feature in object-oriented programming (OOP) that allows new classes to be created from existing ones. The new classes, known as derived classes, inherit attributes and methods from the existing classes, known as base classes. This helps in reusing code and creating a hierarchy of classes.
Composition, on the other hand, is a way to combine objects or classes together to represent a has-a relationship. For example, a car has an engine, and a person has a heart.
Composition
In composition, we create objects that are made up of other objects. This allows us to create complex objects from simpler ones.
class Robot: def __init__(self, name): self.name = name self.sensors = Sensors() # create a class property for sensors, which are defined elsewhere self.motors = Motors() # create a class property for motors, which are defined elsewhere self.controller = Controller() # create a class property for controller, which are defined elsewhere def move_forward(self): self.motors.move_forward() def move_backward(self): self.motors.move_backward() def turn_left(self): self.motors.turn_left() def turn_right(self): self.motors.turn_right() def read_sensor(self): return self.sensors.read_sensor()
In MicroPython, classes can inherit from multiple base classes. This is known as multiple inheritance. When a class inherits from multiple classes, it inherits attributes and methods from all the parent classes.
class A: def method_a(self): print("Method A") class B: def method_b(self): print("Method B") class C(A, B): def method_c(self): print("Method C") c = C() c.method_a() c.method_b() c.method_c()
In MicroPython, inheritance helps create new classes with additional functionalities, building on existing ones.
Remember the Memory Constraints of Microcontrollers When using inheritance in MicroPython, remember that microcontrollers have limited memory. Be cautious when creating new classes that inherit from existing ones, especially with multiple inheritance, as this can increase the memory usage of your program.
When using inheritance in MicroPython, remember that microcontrollers have limited memory. Be cautious when creating new classes that inherit from existing ones, especially with multiple inheritance, as this can increase the memory usage of your program.
Imagine you are building a library for controlling different types of sensors. You can create a base class for a generic sensor and then create derived classes for specific sensors like temperature and humidity sensors. This way, you can reuse the common functionality in the base class while adding specific features in the derived classes.
Encapsulation and Inheritance also help create abstractions in MicroPython. We will explore these concepts in the following lessons.
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 >