Decorators in Python

Decorators provide additional functionality to the functions without directly changing their definition of them. Basically, it takes the functions as an argument, adds functionality to them, and returns it. 

Before diving deep into the concept of Decorators, let's first try to understand 

What are functions in Python and what is an inner function?

In Python everything is Objects, be it Class, Variables, and Functions. So functions are python first-class objects that can be used or passed as an argument. You can store the functions in variables, you can pass a function to another function as parameters, and you can also return the function from the function.

Below is one simple example where we are treating functions as objects.

def  make_me_lowercase(str):
      return str.lower()

print(make_me_lowercase("HELLO World"))

copy_of_you = make_me_lowercase

print(copy_of_you("HELLO World"))

The output of the above calls for both the functions will be the same.

hello world
hello world


Now let's look at one more example where we pass functions as arguments to other functions.

def make_me_lowercase(str):
   return str.lower()

def make_me_uppercase(str):
   return str.upper()

def change_case(func):
   print(func("HELLO World"))

change_case(make_me_lowercase)
change_case(make_me_uppercase)

The output for the above code will be

hello world
HELLO WORLD


What is an inner function?

Python provides an option of defining a function inside another function. These types of functions are called inner functions. Below is one example to understand it better.

def func():  

     print("I am first function")  

     def func1():  
        print("I am first child of first function") 
 
     def func2():  
        print("I am second child of first function")  

     func1()  
     func2()  

func()  

The output of the above code will be

I am first function
I am first child of first function
I am second child of first function


Now coming back to Decorators. Below is one simple example to understand it better.

def decorate_me(func):
   def inner():
      print("I am decorated function")
      func()
   return inner

def simple():
   print("I am simple function")

decorate_me = decorate_me(simple)

Here we defined function simple(), and one decorator called decorate_me(). It is clear that the decorator is taking the function simple() as an argument and adding functionality to it and returning it. Here inner() function acts as a wrapper function in which the original function is called.

We can also use the @ symbol, add the name of the decorator with it, and place it on top of the definition of the function that is to be decorated

@decorate_me
def simple():
   print("I am simple function")

This function was non-parameterized. Now let's check what happens when the function has parameters.


Decorating Functions with Parameters

When the function has parameters, basically nothing changes except now the decorator inner function has to be parameterized as well. Below is one simple example

def sum_it(func):  
   def inner(*args, **kwargs):   
      return func(x,y)  
   return inner  
  
@sum_it  
def sum(x,y):  
   print(x+y) 


Here we can see that the inner function takes the argument as *args and **kwargs which is a tuple of positional arguments or a dictionary of keyword arguments of any length. This makes it a general decorator that can decorate a function with many arguments.

Comments

Popular posts from this blog

Tricky Questions or Puzzles in C

Program to uncompress a string ie a2b3c4 to aabbbcccc

Number series arrangement puzzles