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
42% Percent Complete
By Kevin McAleer, 3 Minutes
In MicroPython, Polymorphism means the ability of an object to take on many forms. This allows us to use a single interface to represent different types of objects. Polymorphism, meaning “occurring in many forms,” is a key concept in object-oriented programming (OOP).
Polymorphism
Polymorphism is the ability of different objects to respond, each in its own way, to identical messages (methods). In simpler terms, polymorphism allows the same method to be used on different objects, each having its own implementation of the method.
Consider a scenario where we have different classes for different types of animals. Each animal can make a sound, but the sound is different for each animal.
class Dog: def make_sound(self): return "Bark" class Cat: def make_sound(self): return "Meow" class Cow: def make_sound(self): return "Moo" # Using polymorphism def animal_sound(animal): print(animal.make_sound()) dog = Dog() cat = Cat() cow = Cow() animal_sound(dog) # Outputs: Bark animal_sound(cat) # Outputs: Meow animal_sound(cow) # Outputs: Moo
In the example above, the animal_sound function uses polymorphism to call the make_sound method on different animal objects. Each object responds to the make_sound method in its own way.
animal_sound
make_sound
Imagine a media player application that can play different types of media files (audio, video, etc.). Each media type has its own way of being played, but the media player can use a single interface to play any type of media file.
class Audio: def play(self): return "Playing audio" class Video: def play(self): return "Playing video" class Image: def play(self): return "Displaying image" # Using polymorphism def play_media(media): print(media.play()) audio = Audio() video = Video() image = Image() play_media(audio) # Outputs: Playing audio play_media(video) # Outputs: Playing video play_media(image) # Outputs: Displaying image
In this example, the play_media function uses polymorphism to play different types of media files. Each media type responds to the play method in its own way.
play_media
play
Polymorphism and inheritance are closely related concepts in OOP. While inheritance allows classes to inherit properties and methods from a parent class, polymorphism allows different classes to be treated as instances of the same class through a common interface.
For example, we can have a base class Animal with a method make_sound, and different animal classes (e.g., Dog, Cat) inherit from the base class and implement their own version of make_sound.
Animal
Dog
Cat
class Animal: def make_sound(self): raise NotImplementedError("Subclasses must implement this method") class Dog(Animal): def make_sound(self): return "Bark" class Cat(Animal): def make_sound(self): return "Meow" # Using polymorphism with inheritance def animal_sound(animal): print(animal.make_sound()) dog = Dog() cat = Cat() animal_sound(dog) # Outputs: Bark animal_sound(cat) # Outputs: Meow
Polymorphism in MicroPython allows objects to take on many forms and enables a single interface to represent different types of objects. It enhances flexibility, maintainability, and extensibility in code. Understanding and using polymorphism is crucial for effective object-oriented programming.
< Previous Next >