Decorators in Python

Lavlesh Singh
3 min readAug 22, 2021

Decorator is a function which can take a function as an argument and extend its functionality and returns modified function with extended functionality.

Input Function — — —> Decorator — — → Add some new Functionality

The main objective of decorator functions is we can extend the functionality of existing functions without modifies that function.

Photo by laura adai on Unsplash

Decorators are very powerful and useful tool in Python since it allows programmers to modify the behavior of function or class. Decorators allow us to wrap another function in order to extend the behavior of the wrapped function, without permanently modifying it. But before diving deep into decorators let us understand some concepts that will come in handy in learning the decorators.

Consider the below examples for better understanding.

Example 1: Treating the functions as objects.

Important Point need to remember

  • A function is an instance of the Object type.
  • You can store the function in a variable.
  • You can pass the function as a parameter to another function.
  • You can return the function from a function.
  • You can store them in data structures such as hash tables, lists.

def f1(text):
return text.upper()
print(shout(‘Hello’))
v= f1
print(v(‘Hello’))

In the above example, we have assigned the function f1 to a variable. This will not call the function instead it takes the function object referenced by F1 and creates a second name pointing to it, v.

Example 2: Passing the function as argument.

def f1(text):
return text.upper()

def f2 (text):
return text.lower()

def greeting(func):
# storing the function in a variable
greeting = func(“””Hi, Good Morning ”””)
print (greeting)

greeting(f1)
greeting(f2)

In the above example, the greet function takes another function as a parameter (shout and whisper in this case). The function passed as argument is then called inside the function greet.

Example 3: Returning functions from another functions.

def create_adder(x):
def adder(y):
return x+y
return adder
add_15 = create_adder(15)
print(add_15(10))

In the above example, we have created a function inside of another function and then have returned the function created inside.

Example 4: Using Decorators in below function.

As stated above the decorators are used to modify the behavior of function or class. In Decorators, functions are taken as the argument into another function and then called inside the wrapper function.

Will start understanding with simple one !

def greeting(name):
print(“Hello”,name,”Good Morning”)

This function can always print same output for any name
Hello John Good Morning
Hello Ravi Good Morning
Hello Sunny Good Morning

But we want to modify this function to provide different message if name is Sunny.
We can do this without touching wish() function by using decorator.

def decor(func):
def inner(name):
if name==”Sunny”:
print(“Hello Sunny Bad Morning”)
else:
func(name)
return inner

@decor
def greeting(name):
print(“Hello”,name,”Good Morning”)

greeting(“John”)
greeting(“Ravi”)
greeting(“Sunny”)

Output
Hello John Good Morning
Hello Ravi Good Morning
Hello Sunny Bad Morning

In the above program whenever we call greeting() function automatically decor function will be executed.

How to call same function with decorator and without decorator:

def decor(func):
def inner(name):
if name==”Sunny”:
print(“Hello Sunny Bad Morning”)
else:
func(name)
return inner

def greeting(name):
print(“Hello”,name,”Good Morning”)

decorfunction=decor(greeting)

greeting(“John”) #decorator wont be executed
greeting(“Sunny”) #decorator wont be executed

decorfunction(“John”)#decorator will be executed
decorfunction(“Sunny”)#decorator will be executed

Output
Hello john Good Morning
Hello Sunny Good Morning
Hello John Good Morning
Hello Sunny Bad Morning

Decorator Chaining :-

We can define multiple decorators for the same function and all these decorators will form Decorator Chaining.

Eg:
@decor1
@decor
def num():
For num() function we are applying 2 decorator functions. First inner decorator will work
and then outer decorator.

Example.

def decor1(func):
def inner():
x=func()
return x*x
return inner

def decor(func):
def inner():
x=func()
return 2*x
return inner

@decor1
@decor
def num():
return 10
print(num())

Demo Program for decorator Chaining:

def decor(func):
def inner(name):
print(“First Decor(decor) Function Execution”)
func(name)
return inner

def decor1(func):
def inner(name):
print(“Second Decor(decor1) Execution”)
func(name)
return inner

@decor1
@decor
def greeting (name):
print(“Hello”,name,”Good Morning”)

greeting (“john”)

Thanks for Reading this blog !!

https://medium.com/analytics-vidhya/java-vs-scala-7ff9eb50141

--

--