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
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
Pi Tray - Mini-rack - Part II
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
48% Percent Complete
By Kevin McAleer, 3 Minutes
In MicroPython, a decorator is a function that takes another function as an argument and extends its functionality without modifying it.
Decorators are a powerful tool that allows us to add functionality to a function without changing its code. They are a way to wrap a function, modifying its behavior.
Decorators are a way to wrap a function, modifying its behavior. They take another function as an argument and extend its functionality without modifying the original function.
Here is a simple example of a decorator:
def my_decorator(func): def wrapper(): print("Something is happening before the function is called.") func() print("Something is happening after the function is called.") return wrapper def say_hello(): print("Hello!") say_hello = my_decorator(say_hello) say_hello()
In the example above, we define a decorator my_decorator that takes a function func as an argument. The wrapper function is used to wrap the func function, adding functionality before and after the function is called.
my_decorator
func
wrapper
We then apply the decorator to the say_hello function by reassigning say_hello to the result of calling my_decorator(say_hello).
say_hello
my_decorator(say_hello)
@
In Python, we can use the @ symbol to apply a decorator to a function. This is a more concise way of applying a decorator.
Here is the same example using the @ symbol:
def my_decorator(func): def wrapper(): print("Something is happening before the function is called.") func() print("Something is happening after the function is called.") return wrapper @my_decorator def say_hello(): print("Hello!") say_hello()
The @my_decorator syntax is a shorthand for applying the decorator to the function.
@my_decorator
In MicroPython, we can use decorators to create setters and getters for class properties. Setters and getters control access to class properties, ensuring values are valid and within a specified range.
Here is an example of a class with a setter and getter:
class Robot: def __init__(self, name): self.name = name self.__speed = 0 @property def speed(self): return self.__speed @speed.setter def speed(self, value): if value < 0: self.__speed = 0 elif value > 100: self.__speed = 100 else: self.__speed = value r = Robot("Robbie") r.speed = 50 print(r.speed) r.speed = 150 print(r.speed) # notice the speed was capped at 100
In this example, the @property decorator is used to define a getter method for the speed attribute, and the @speed.setter decorator is used to define a setter method.
@property
speed
@speed.setter
Don’t Go Crazy with Property Decorators There is a temptation to use property decorators for every class property. This is not necessary and can make your code harder to read. Only use property decorators when you need to control access to a class property.
There is a temptation to use property decorators for every class property. This is not necessary and can make your code harder to read. Only use property decorators when you need to control access to a class property.
Decorators in MicroPython are a powerful feature that allows you to extend the functionality of functions and methods without modifying their code. By using decorators, you can create more reusable, organized, and maintainable code.
< Previous Next >