What is a Python decorator?
Yamil Garcia
Tech enthusiast, embedded systems engineer, and passionate educator! I specialize in Embedded C, Python, and C++, focusing on microcontrollers, firmware development, and hardware-software integration.
By definition, a decorator is a function that takes another function and extends the behavior of the latter function without explicitly modifying it.
A decorator is just a regular Python function.
Put simply:?decorators wrap a function, modifying its behavior.
Python allows you to?use decorators in a simpler way with the?@?symbol, sometimes called the?“pie” syntax. The following example shows the syntax of a simple decorator.
So,?@my_decorator?is just an easier way of saying?say_whee = my_decorator(say_whee). It’s how you apply a decorator to a function.
Can you guess what happens when you call?say_whee()? Try it:
You can now use this new decorator in other files by doing a regular?import.
To learn more about Python decorators, check this excellent tutorial on Real Python site
#python #realpython #programming