Learn Python Decorators in 2 minutes
Decorators in python allow the programmer to modify the behaviour of functions. They wrap another function in order to extend the behaviour of a wrapped function, without permanently modifying it.
Think of decorator as a function to which you pass another function to add or modify some of its functionality. A concrete example of decorator can be a function that divides two numbers
In the above example, the divide function will work fine when called with values (12, 2) but with values (12, 0) it will give a division by zero error. To handle this case, we can use a decorator like this
Now by using the check_divide decorator, we are making sure that the divide function doesn’t throw any error. Working of a decorator
A decorator function takes a function as its argument. Inside this decorator function, you declare another function with the same arguments as that of the function passed to the decorator function. Whatever you want to append to your original function, you write inside this inner function and then return the passed function.
Finally, return the inner function to complete defining your decorator.
To take another example, suppose you create a function to enter a name
Now you need to print this name in a sentence but you don’t want to edit the function. Therefore, you need to create a decorator
In this manner, you can use decorators in your python code.