Hello All, hope you are doing well in your job as well as life. Let’s learn something new and exciting today i.e. Python Decorators. I saw lot of people are confused with Decorators and its usage. So, in this article we will see what Python Decorator is and why we should use it.
What is Python Decorator?
To add some functionality to an existing code, Python came up with a very interesting feature known as Decorator or we can simply call it as metaprogramming. You might be thinking why we call it as “metaprogramming”. The answer is very simple, because it allows you to modify the behavior of functions.
To put up in simple words, “Decorators are the functions which modify the functionality of another function.”
We all know that everything in Python is an object. I assume we all have basic knowledge of Python and understand the concepts of functions and object in python. Keeping in mind these pre-requisites let’s start with some coding.
Let’s take our first step to create Decorator and define a function.
first_func(“Welcome to BeingDatum”)
second_func = first_func
second_func(“Welcome to BeingDatum”)
When we run this code, we will get the same output. This means that here we have two names for the same function i.e. first_func and second_func.
Now, we will move to our next step i.e. defining functions within functions. Yes, you read it correctly in Python we can define functions inside functions.
print(“Welcome to BeingDatum”)
print(“This is my_first function”)
print(“I will call second function now”)
Output will be:
Welcome to BeingDatum
This is my_first function
I will call second function now
Functions and methods are callable. In fact, any object which implements __call__() method is termed callable. Hence, decorator is on the same concept i.e. it is a callable that returns a callable. Let’s write our first decorator code:
print(“I am first”)
print(“I am second”)
When you run the command, sec_decorator(), output will be:
I am second
Now, let’s decorate this function:
Decorate = first_decorator(sec_decorator)
I am first
I am second
Here, our first_decorator got decorated and returned function was assigned the name as Decorate.
We see that decorator function added some functionality to original function.
Let’s take another example of decorator:
def divide(a, b):
Suppose if I pass value of b as 0, we will get an error i.e. Divide by Zero error.
Now, we will use decorator to handle this error.
def division(a, b):
print(“Divide a and b”)
if b == 0:
print(“This is an error, Zero by Division error”)
return func(a, b)
@Decorator to divide without the error
def divide(a, b):
In above example, we see that if we pass the parameters as (32,2) output will be there but (32, 0) then no output.
Working of a Decorator:
A decorator function takes a function as its argument. Inside this decorator function, you declare another function with same parameters as of the other function. Now, whatever you need to amend or change to your original function, you write inside the inner function and return the passed/function and finally return it.
Let’s take one more example here to clear our understanding:
Now, the requirement is to add your name in some sentence but you don’t want to change existing functionality.
So, this is how we can use Decorators in Python. Please provide your feedback or comments or contact us at email@example.com