✆+91-9916812177 | contact@beingdatum.com

Introduction to Python Decorators

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.

def first_func(msg):
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.

def my_first():
def sec_func():
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:

def first_decorator(func):
def inner():
print(“I am first”)
return inner
def sec_decorator():
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):
return 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 handle_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)
return division()

@Decorator to divide without the error
def divide(a, b):
return 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:

def your_name(name):
return name

Now, the requirement is to add your name in some sentence but you don’t want to change existing functionality.

def sentence(func):
def inner(x):
print(“Hello, “+x)
return func(s)
return inner

def say_name(name):
return name


So, this is how we can use Decorators in Python. Please provide your feedback or comments or contact us at contact@beingdatum.com


May 12, 2020

0 responses on "Introduction to Python Decorators"

    Leave a Message

    Your email address will not be published. Required fields are marked *

    © BeingDatum. All rights reserved.