Article Image
Article Image
read
A decorator is a function that takes another function as a input, and does something with it. Say we have two functions:
Function B is a decorator. Running this would return:
Decorators can be used for many things like monitoring the output of functions or calling a function multiple times
Pie Syntax
Decorator functions are usually called using Pie syntax. It looks like this:
This is the python way of doing:
Real Example
Flask
Flask is a Python application framework. Decorators are used in Flask web controllers to map URLs to the functions they perform. For example:
If a user went to the URL /home the decorator app.route() would execute the function returning “You are home”.