Introduction

Setup

Operators

variable

String

Number

Dtype & Comment

Lists

Tuples

Sets

Dictionaries

if else statement

For Loop

While Loop

iterator & iterable

Pre-Built functions

Function

Decorators

Generators

Types of errors

Exceptions Handling

OOP

Regular expression

Create module

OS module

Date module

Random module

Math

Statistics module

Exercise 1

Exercise 2

Exercise 3

Learn Data Structure & Algorithm

Learn Numpy

Learn Pandas

Learn Matplotlib

Learn Seaborn

Learn Statistics

Learn Math

Learn MATLAB

Learn Machine learning

Learn Github

Learn OpenCV

Learn Deep Learning

Learn MySQL

Learn MongoDB

Learn Web scraping

Learn Excel

Learn Power BI

Learn Tableau

Learn Docker

Learn Hadoop

Python decorators

Why decorators are needed?

Suppose we have two functions(func1, func2). Func1 will print "I'm from func1" and func2 will print "I'm from func2".
Suppose these functions are already created. But after creating the function, now we also want that when each function gives the output that time they also print another thing and that is "This function is very good". But the problem is we already wrote the code and we don't want to do changes inside the code but we also want to add something new.
Now what we will do?
In this case, we will use decorators.

Example:
Input
def decorator_func(take_function):
  def wraper_function():
    print("This function is a very good ")
    return take_function()
  return wraper_function

def func1():
  print("I'm form func1")

def func2():
  print("I'm form func2")

var1=decorator_func(func1)
var1()

var2=decorator_func(func2)
var2() Output
This function is a very good
I'm form func1
This function is a very good
I'm form func2

In the example, when we called the decorator function and the decorator function takes a parameter which is the function. So we pass the func1 function as a parameter. So first decorator_func will be called and this function will return another function and that is wraper_function. Now, this wrapper function is stored in var1 and var2 variables. So when we call var1, it means we are calling the wrapper function. So after calling var1 wraper_function will run and first it will print "This function is very good", then it will work with the parameter value. Here we pass a function as a parameter. So when the wrapper function will start working with the parameter it will run the function given in the parameter and will print "I form func1".We always have to return wraper_function and take_function.

Shortcut of decorator

To use the decorators shortcut, we have to write @decorator_function_name before the main function.

Example:1
Input
def decorator_func(take_function):
  def wraper_function():
    print("This function is a very good ")
    return take_function()
  return wraper_function

@decorator_func
def func1():
  print("I'm form func1")

@decorator_func
def func2():
  print("I'm form func2")

func1()
func2()
Output
This function is a very good
I'm form func1
This function is a very good
I'm form func2

In example 1 we have two main functions (func1,func2) and a decorator function. Here we wrote @decorator_func before func1 and func2. By doing this we don't have to create a variable and then call the decorator function and also don't need to pass the main function as a parameter. If we just give the call main function then the decorator main function will automatically execute together and will give the output. If we do this then the first decorator function will execute and then the main function will be executed.

Example:2
Input
def decorator_func(take_function):
  def wraper_function(*args,**kwargs):
    print("This function is a very good ")
    return take_function(*args,**kwargs)
  return wraper_function

@decorator_func
def func1(a,b):
  v=a+b
  print("I'm form func1",v)

@decorator_func
def func2(a,b):
  v=a+b
  print("I'm form func2",v)

func1(5,6)
func2(7,3)
Output
This function is a very good
I'm form func1 11
This function is a very good
I'm form func2 10

If we want to use parameters and pass a value of the parameter to the main function(func1, func2) then we have to write *args and **kwargs in wrapper function and take_function.

CodersAim is created for learning and training a self learner to become a professional from beginner. While using CodersAim, you agree to have read and accepted our terms of use, privacy policy, Contact Us

© Copyright All rights reserved www.CodersAim.com. Developed by CodersAim.