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.
We then apply the decorator to the say_hello
function by reassigning say_hello
to the result of calling my_decorator(say_hello)
.
@
SymbolIn 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.
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.
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.
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.
You can use the arrows β β
on your keyboard to navigate between lessons.