Decorators in Python

Python Decorators

In Python, a decorator is a special type of function that can be used to modify the behaviour of another function.

Define a decorator function

A decorator takes a function as input, adds some functionality to it, and then returns the modified function.

In the following example, mydecorator() is a decorator function, which takes a function fn as argument, the inner_function() adds some functionality of the function fn, and then the decorator function mydecorator() returns the inner_function().

def mydecorator(fn):
    def inner_function():        
        print('Hello! ', end='')
        fn()
    return inner_function

Decorators are typically used to add features to a function without changing the function’s code, or to apply a common behavior to multiple functions in a program.

As in the above example, inner_function() did not modify the code in fn(), but it extended the behaviour of fn() by a print statement before calling the fn().

Apply decorator to a function

In Python, decorators are applied to a function by using the “@” symbol, followed by the decorator function’s name, just above the function to be decorated.

In the following example, we have decorated the somefunction() with mydecorator.

@mydecorator
def greet():
    print('Good morning!')

When the decorated function somefunction() is called, the decorator function mydecorator() is executed first, and then the modified function is called.

Decorator Example

In the following program, we define a decorator function, mydecorator(), then define another function greet(), and decorate the greet() function with the decorator @mydecorator.

Python Program

def mydecorator(fn):
    def inner_function():        
        print('Hello! ', end='')
        fn()
    return inner_function

@mydecorator
def greet():
    print('Good morning!')

greet()
Run Code Copy

Output

Hello! Good morning!

Decorator with Arguments

In the above discussion, we did not let our decorator function take any arguments. Now, you shall learn how to enable the decorator function to accept arguments, and pass those arguments to the decorated function.

In the following program, to access the arguments that we pass to the greet() function in the decorator function, we first take those passed values as arguments to the inner_function(), and then pass them to the fn() using fn(*args, **kwargs).

Python Program

def mydecorator(fn):
    def inner_function(*args, **kwargs):        
        print('Hello! ', end='')
        fn(*args, **kwargs)
    return inner_function

@mydecorator
def greet(name):
    print(name)

greet('Ram!')
Run Code Copy

Output

Hello! Ram!

Uses of decorators

Decorators can be used for a variety of purposes, such as logging, timing, or authentication, among others.

Types of Decorators

Please note that there are other types of decorators in Python, not just Function decorators. But since, function decorators are commonly used, and other decorators are less-commonly used, we have covered only function decorators in this tutorial.

The following are the complete list of decorators in Python.

  1. Function Decorators: These are the most common type of decorators and are applied to functions. They modify the behavior of a function by wrapping the function in another function.
  2. Class Decorators: These decorators modify the behavior of a class. They are applied to a class and modify the class or its methods.
  1. Method Decorators: These are applied to individual methods within a class and modify the behavior of that method.
  2. Property Decorators: These are applied to a class property and modify the behavior of that property.

Other Tutorials

The following are some of the other tutorials that cover decorators.

Summary

In this tutorial, we learned what decorators are, how to define them, different kinds of decorators, and examples for all of these.

Related Tutorials

Code copied to clipboard successfully 👍