From this , you will be learning about Python Decorator. You will see what decorators are, how to create and use them with the help of examples.

Note: The syntax used here is for Python 3. You may modify it to use with other versions of Python.

Python Decorator

To Learn Python from Scratch – Read Python Tutorial

What is Python Decorator?

Decorators are a callable entity in Python that allows us to make modifications to functions or classes. The decorator works in an abstract style to extend or completely replace the behavior of an object.

By understanding how to create decorators, you make your code extensible and more readable. Also, you get the functions tailored to your needs without even changing their definitions.

Decorator syntax

Follow the below style:

def decoratorFunc(args):
    # Code to you want to execute

def existingFunction(args):
    # Code that you want to decorate

Alternatively, you can try this way:

existingFunction = decoratorFunc(existingFunction)

How does Decorator work in Python?

Mainly, decorators are a construct that wraps a function, gives it a new functionality without changing the original code.

They return the output which may differ from the original behavior of the function.

They are good to use when a function has to operate differently in different situations.

Create your decorator:

A decorator is a function that may or may not return a function object.

We place it on of the function that we want to decorate and pre-fix it with a @ sign.

So, let’s first create the decorator function.

def decoratorFunc(fn):

The above decorator function returns an integer instead of a function.

So when you apply it to a function, it would get overridden altogether.

Check below how to specify a decorator to a function.

def existingFunc():
    print("Hello World!")

Let’s now bring all the pieces together.

def decoratorFunc(fn):
    return 10

def existingFunc():
    print("Hello World!")


When you the above code, the following error occurs.

line 8, in 
TypeError: 'int' object is not callable

It is because the decorator replaced the existing function and forcing it to return “10” which is an integer, not a callable object.

NOTE – the decorator’s return value replaces the function, i.e., existingFunc.

By the way, if you want to run the function that we decorated, then make the decorator return it. Check the below code.

def decoratorFunc(fn):
    def callExistingFunc():
        print("%s was called." % fn)
    return callExistingFunc

def existingFunc():
    print("Hello World!")


In the above example, our decorator function is returning a function which prints the name of the decorated function and executes it.

The result of execution is as follows:

<function existingFunc at 0x0000000000705158> was called.
Hello World!


The following diagram attempts to simplify the decorator concept for you.

Decorator flow in python  - Decorator flow in python - Python Decorator Tutorial &#8211; Learn to Use with Examples

Must Read – Functions in Python

Chaining Decorators

We can decorate a function as many times as desired. In such a case, the decorators create a chain effect.

Typically, the decorator at the top hands over the control to the next, and continues in this way.

For illustration, check out the following code:

def top(func):
    def wrapper(*args, **kwargs):
        print("1" * 1)
        func(*args, **kwargs)
        print("1" * 1)
    return wrapper

def middle(func):
    def wrapper(*args, **kwargs):
        print("2" * 2)
        func(*args, **kwargs)
        print("2" * 2)
    return wrapper

def bottom(func):
    def wrapper(*args, **kwargs):
        print("3" * 3)
        func(*args, **kwargs)
        print("3" * 3)
    return wrapper

def myTest(anyString):

myTest("Hello World!")

You can check that this example is using three decorators on the myTest() function. Below is the result after execution:

Hello World!

Decorator Examples

Simple decorator program to demonstrate:

def decorate(func):
   def first():
      print("This is the First Program on Decorators.")
   return first

def hello_not_decorated():
   print("Hello World!.n")

print("This is an original function that is not decorated : ")

print("This is a decorated function :")
def hello():
   print("Hello World!.")


#1 Output:

This is an original function that is not decorated : 
Hello World!.

This is a decorated function :
Hello World!.
This is the First Program on Decorators.

Decorate arithmetic operations:

def arithmetic_operations(func):
    def operators(a, b):
        func(a, b)
        print("The product is :", a*b)
        print("The division is :", a/b)
        print("The remainder is :", a%b)
    return operators
print("This is a decorated function.")

def add_and_subtract(a, b):
    print("The addition is :", a + b)
    print("The subtraction is :", a - b)

add_and_subtract(8, 4)

#2 Output:

This is a decorated function.
The addition is : 
The subtraction is : 4
The product is : 32
The division is : 2.0
The remainder is :

Display multiple lines using chaining:

def Chaining_of_decorators(func):
   print("This is an example of chaining of decorators implementation.")
def Decorator_demo(func):
    print("This tutorial is about Decorators.")
print("This is a decorated function.")

def hello():
   print("Hello World!")

#3 Output:

This is a decorated function.
This tutorial is about Decorators.
This is an example of chaining of decorators implementation.

Pass arguments to a decorator:

def argument_for_decorator(argument1, argument2):
    def decorator(function):
        def wrapper(*args):
            print("%s%s" % (argument1, argument2))
            print("Congratulations.  You decorated a function.")
        return wrapper
    return decorator

@argument_for_decorator("Hello ", "World!")
def print_args(*args):
    print("The Fibonacci Sequence upto number 8.")
    for arg in args:

print_args(1, 1, 2, 3, 5, 8)

#4 Output:

Hello World!
The Fibonacci Sequence upto number 8.
Congratulations.  You decorated a function.

We wish the above Python Decorator tutorial would have given you a fair idea of using them in real Python programs.



Source link


Please enter your comment!
Please enter your name here