Course Content

Decorators In Python

In this tutorial, you will learn how you can create a decorator and why you should use it. Before you understand decorators, make sure you know about how Python functions work. If you don’t know about python function, then check our Functions And Docstrings tutorial, because functions are the fundamental concept in understanding python decorators. Functions in Python can be defined as lines of codes that are built to create a specific task and can be used again and again in a program when called.


Figure1: Functions in Python

What is Python Decorator?

Decorator as can be noticed by the name is like a designer that helps to modify a function. The decorator can be said as a modification to the external layer of function, as it does not make any change in its structure. What a decorator does is, it takes a function and insert some new functionality in it without changing the function itself. A reference to a function is passed to a decorator and the decorator returns a modified function. The modified functions usually contain calls to the original function. This is also known as metaprogramming because a part of the program tries to modify and add functionality into another part of the program at compile time. Understanding the definition could be difficult but we can grasp the concept easily through the example in the video section. In terms of python, the other function is also called a wrapper.

wrapper is a function that provides a wrap-around another function. While using decorator all the code which are executed before our function that we passed as a parameter and also the code after it is executed belongs to the wrapper function. The purpose of the wrapper function is to assist us. Like if we are dealing with a number of similar statements, the wrapper can provide us with some code that all the functions have in common and we can use a decorator to call our function along with wrapper. A function can be decorated many times. 

Note that a decorator is called before defining a function.

There are two ways to write a Python decorator:

  • We can pass our function to the decorator as an argument, thus define a function and pass it to our decorator.
  •  We can simply use the @ symbol before the function we'd like to decorate.
def inner1(func): 
    def inner2():
        print("Before function execution"); 
        print("After function execution")    
    return inner2 

def function_to_be_used(): 
    print("This is inside the function") 



• Decorator function can make our work compact because we can pass all the functions to a decorator that requires the same sort of code that the wrapper provides.
• We can get our work done, without any alteration in the original code of our function.
• We can apply multiple decorators to a single function.
• We can use decorators in authorization in Python frameworks such as Flask and Django, Logging and measuring execution time.

We can do a lot with decorators like Multiple decorators that can be applied to a single function. I hope this tutorial serves as a good introduction to decorators in Python. After understanding the basics of Python decorator, learn more advanced use cases of decorators, and how to apply them to classes.

Code file as described in the video

# def function1():
#     print("Subscribe now")
# func2 = function1
# del function1
# func2()

# def funcret(num):
#     if num==0:
#         return print
#     if num==1:
#         return sum
# a = funcret(1)
# print(a)

# def executor(func):
#     func("this")
# executor(print)

def dec1(func1):
    def nowexec():
        print("Executing now")
    return nowexec

def who_is_harry():
    print("Harry is a good boy")

# who_is_harry = dec1(who_is_harry)




deva89 5 months, 4 weeks ago
1.) Multiple decorators how will they work
2.)decorator for logging
# decorator

def split_str(func):
  def wrapper(*args,**kwargs):
    result = func(*args,**kwargs).split()
    return result
  return wrapper

def to_Upper(func):
  def wrapper(*args,**kwargs):
    result = func(*args,**kwargs).upper()
    return result
  return wrapper

def hello(val):
  return val

# func = to_Upper(hello)
# print(func("hello world I am deva"))


print(hello("deva is a good boy"))


No downloadable resources for this video. If you think you need anything, please post it in the QnA!

Course Announcements

Any Course related announcements will be posted here