Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
Python Decorators is a powerful tool to add functionality to functions or classes without modifying their original code. A decorator is essentially a function that takes another function as an input and adds some functionality to it and then returns the original function with the added behavior.
Let’s look in detail at Python decorators in this article.
What are Decorators in Python?
Decorators are a useful tool in Python that helps us to modify the behavior of a class or function. Python decorators are implemented as a function that takes a function as an argument and returns a new function that wraps the original function.
In order to use decorators, we need to place the decorator function above the function or class it modifies and annotate it with the symbol "@" followed by the name of the decorator function.
But before learning decorators, we need to understand some concepts.
What are the functions in Python?
A function is a handy block of code designed to do a specific job you might need to do often. It's like a tool you can use to avoid writing the same code again and again for different situations. Instead of repeating yourself, you put that code inside a function and call the function whenever you need it.
Functions are like building blocks that make your code neat and organized. They help you use the same code in different places without copying and pasting, making your work more efficient.
In Python, there are already available for you to use, and you can also create your own functions to do things your way. This makes your programs more flexible and easier to manage.
First Class Objects
Functions are treated as first-class objects since they can be treated like any other object. This means that functions can be assigned to variables, passed as arguments to other functions, and returned as values from functions.
Treating Functions as Objects
Python
Python
def add(a, b): return a + b
func = add
result = func(2, 3) print(f"The result of the add function is {result}");
You can also try this code with Online Python Compiler
In this example, the add() function returns another function called inner(). The inner() function takes an argument y and returns the sum of x.
Inner Functions
A function can be defined within other functions. These are referred to as inner functions or nested functions. These functions are within the scope of another function.
Inner functions have access to the variables of outer functions.
Inner functions allow for encapsulating related functionality within a larger function.
Outer functions do not have access to inner function’s variables.
Python decorators also use inner functions.
Python
Python
def first(): def second(): print("This is the second function.") print("This is the first function.") second()
first()
You can also try this code with Online Python Compiler
In this example, the outer() function contains an inner function called inner(). First, the outer function is called, and then the inner function is called.
These examples are enough to understand Decorators in Python. Now we will look at the syntax used by Decorators.
Syntax of Python Decorators
Python
Python
def decorator(original): def wrapper(*args, **kwargs): # To execute before the original function is called result = original(*args, **kwargs)
# To execute after the original function is called return result return wrapper
@decorator # Code for the original function def func(): pass
You can also try this code with Online Python Compiler
In the above example, “decorator” takes another function, “original” as an argument and returns another function, “wrapper” that adds some functionality before and after the original function. @decorator applies decorator to func, replacing func with a wrapper that includes decorator’s behavior.
Examples of Python Decorator
Now we will look at some of the examples of the Python Decorator function to understand it more clearly.
Decorating Function with Parameters
Here we will look at a python decorator function that multiplies two numbers with a wrapper function that checks for the special case of multiplying by zero.
Python
Python
# Define a decorator function def multiply(original): # Define a function that wraps the original function def wrapper(a, b): print("Let's multiply", a, "and", b, "with decorator") # Check if either a or b is 0 if b == 0 or a == 0: print("The answer is 0") return # Call and return its result return original(a, b) # Return the wrapper function return wrapper
# Define a function that will be # decorated with the multiply function @multiply def multiply_with_decorator(a, b): print("Multiply with decorator: ", a * b)
# No decorator is used def multiply_without_decorator(a, b): print("Multiply without decorator: ", a * b)
# Call the decorated function with some arguments multiply_without_decorator(4, 5) multiply_with_decorator(4, 5)
You can also try this code with Online Python Compiler
The decorator function checks if the arguments of a decorated function are not zero before calling it. We multiply two numbers and print a message before doing that. First, a function is called without decorator. Finally, the decorated function is called with some arguments to demonstrate the use of the decorator.
@staticmethod
The @staticmethod decorator is used to define a method that does not receive an implicit first argument like self or cls. This means the method can be called directly on a class without creating an instance and doesn’t access or modify the class or instance state.
Example
class MathUtility:
@staticmethod
def add(a, b):
return a + b
# Calling static method without creating an instance
result = MathUtility.add(5, 7)
print("Sum:", result)
You can also try this code with Online Python Compiler
The @classmethod decorator defines a method that takes cls (class itself) as the first argument instead of self. It can modify class state and is often used for factory methods or alternative constructors.
Example
class Person:
species = "Human"
def __init__(self, name):
self.name = name
@classmethod
def create_anonymous(cls):
return cls("Anonymous")
# Using class method to create an instance
anon = Person.create_anonymous()
print(anon.name)
You can also try this code with Online Python Compiler
Can be used to create instances or modify class-level data.
Useful for alternative constructors.
@property
The @property decorator allows you to define a method that can be accessed like an attribute. It's typically used to create getter methods that look like regular attributes, enabling encapsulation and data validation.
Example
class Circle:
def __init__(self, radius):
self._radius = radius
@property
def diameter(self):
return self._radius * 2
# Accessing method like an attribute
c = Circle(5)
print("Diameter:", c.diameter)
You can also try this code with Online Python Compiler
We applied two decorators, “add_ten” which adds 10 to the function argument, and “multiply” which multiplies 20 to the function and applies them in reverse order to the function. First, “add_ten” decorator is applied and then the “multiply” decorator.
Here we demonstrate the use of chaining decorators in python.
NOTE: Decorators are normal python functions. Therefore, we can use them in our code as many times as we want.
A function decorator is a higher-order function that takes another function as input and extends its behavior without modifying its structure. Function decorators are commonly used for logging, access control, timing, and more.
Example
def greet_decorator(func):
def wrapper():
print("Hello!")
func()
print("Goodbye!")
return wrapper
@greet_decorator
def say_name():
print("My name is Python.")
say_name()
You can also try this code with Online Python Compiler
@greet_decorator wraps say_name() with extra behavior.
Adds a greeting and farewell without changing the original function.
Useful for reusable logic across multiple functions.
Method Decorators
A method decorator is similar to a function decorator but is specifically used inside classes. It can modify instance methods, class methods, or static methods based on context.
@log_method enhances display() by logging before execution.
Works like function decorators but used in class methods.
Great for logging, debugging, or validation in OOP.
Class Decorators
A class decorator is applied to a class definition. It takes a class as input and returns a modified or extended version of that class, often used for adding attributes or modifying behavior dynamically.
Example:
def add_greeting(cls):
cls.greet = lambda self: print("Hello from the class!")
return cls
@add_greeting
class MyClass:
def show(self):
print("Original behavior.")
obj = MyClass()
obj.show()
obj.greet()
You can also try this code with Online Python Compiler
@add_greeting adds a new method greet() to MyClass.
Used for metaprogramming, API modifications, or class enhancement.
Alters class behavior without changing the source class directly.
Frequently Asked Questions
What are the decorators in Python?
We define a decorator function in Python using “@decorator” syntax before the function it decorates.
What is the role of decorator in Python?
In Python, the role of the decorator is to modify the behavior of functions or methods. They provide a way to cover functions with additional functionality, helping in tasks like logging, authorization, and code preprocessing. Decorators help keep code concise, reusable, and maintainable.
What is the use of decorator in Python class?
In Python classes, decorators are used to change the behavior of methods. They allow the augmentation of method functionality, like adding authentication checks or logging, without altering the original method code. This enables code reusability and separation of concerns.
Conclusion
In this article, we discussed the Python Decorator Function. You can also read the articlePython Introductionto improve your knowledge of Python.