Python Decorators



Python Decorators function is only a function that takes another function as an argument and adds extra features or behavior to another function without changing the source code of the original function.

The python decorators take advantage of the case that the functions in python are the most useful functions it means that we can pass a function as an argument to other functions. We can return functions and assign functions to the variables.

 The function is decorated with the @ symbol

There are 2 kinds of decorators: -  

  • Function decorators
  • Class decorators

Syntax

@my_decorator
def my_function():
    pass

Before you can understand decorators, you must first know how functions work.

Read this article:  Python Functions


Example

# Decorator take another function as arguments
def myDecoratorFunc(func):
    # Inner Function
    def innerFunc():
        print("Denny")
        # another function take as arguments
        func()
        print("Rex")
    return innerFunc

# outer Function
def name():
    print("Venom")

# call Outer function
name()

# Passing value in the variable
names = myDecoratorFunc(name)

# call passing a variable as a function
names()

Output

Venom
Denny
Venom
Rex

Decorator syntax

As an alternative to the manual decorator assignment above, just add @decorator_name before the function that you want to decorate.


Example

# Decorator take another function as arguments
def myDecoratorFunc(func):
    # Inner Function
    def innerFunc():
        print("Denny")
        # another function take as arguments
        func()
        print("Rex")
    return innerFunc

# outer Function
@myDecoratorFunc
def name():
    print("Venom")

# call Outer function
name()

Output

Denny
Venom
Rex

Decorator Functions with Arguments

If our function has input arguments and we try to wrap it with our decorator above, it will raise a TypeError since we have to call our function inside the wrapper with these arguments, too. However, we can fix this by using *args and **kwargs in the inner function:


Example

# Decorator take another function as arguments
def myDecoratorFunc(func):
    # Inner Function
    def innerFunc(*args, **kwargs):
        print("Denny")
        # another function take as arguments
        func(*args, **kwargs)
        print("Rex")
    return innerFunc

# outer Function
@myDecoratorFunc
def age(x):
    return "Age", x


# call Outer function
result = age(21)
print(result)

Output

Denny
Rex
None

Above Example, we can't get a result cause we do not return the value from our inner Function.


Example

# Decorator take another function as arguments
def myDecoratorFunc(func):
    # Inner Function
    def innerFunc(*args, **kwargs):
        print("Denny")
        # another function take as arguments
        res = func(*args, **kwargs)
        print("Rex")
        return res
    return innerFunc

# outer Function
@myDecoratorFunc
def age(x):
    return x


# call Outer function
result = age(21)
print(result)

Output

Denny
Rex
21

Example

# Decorator take another function as arguments
def myDecorator(name="Python"):
    def myDecoratorFunc(func):
        # Inner Function
        def innerFunc():
            print("HTML")
            # another function take as arguments
            func()
            print("CSS")
        return innerFunc
    return myDecoratorFunc

# outer Function
@myDecorator(name="Java")
def subject():
    print("Django")

# call Outer function
subject()

Output

HTML
Django
CSS