Python is known for its simplicity and powerful features that make coding easier and more efficient. One such advanced but very useful concept is decorators. Decorators allow you to modify or enhance functions without changing their actual code.
In this blog, we will understand what decorators are, how they work, and why they are important in Python programming.

What are Decorators in Python?
A decorator in Python is a function that takes another function as input and adds extra functionality to it without modifying its original structure.
In simple words:
👉 Decorators “wrap” another function and extend its behavior.
They are commonly used for:
- Logging
- Authentication
- Timing functions
- Access control
Why Use Decorators?
Decorators are useful because they help you:
- Reuse code easily
- Keep functions clean and simple
- Add extra features without modifying original code
- Follow DRY (Don’t Repeat Yourself) principle
Basic Structure of a Decorator
A decorator is a function inside another function.
Example:
def my_decorator(func):
def wrapper():
print("Before function execution")
func()
print("After function execution")
return wrapper
Here:
funcis the original functionwrapper()adds extra behavior
Using a Decorator
We can apply a decorator using @ symbol.
Example:
def my_decorator(func):
def wrapper():
print("Start")
func()
print("End")
return wrapper@my_decorator
def hello():
print("Hello World")hello()
Output:
Start
Hello World
End
The @my_decorator automatically wraps the hello() function.
How Decorators Work
When Python sees:
@decorator_name
def function():
pass
It internally converts it into:
function = decorator_name(function)
So, decorators simply replace the original function with a modified version.
Decorator with Arguments
Decorators can also work with functions that take arguments.
Example:
def decorator(func):
def wrapper(name):
print("Greeting starts")
func(name)
print("Greeting ends")
return wrapper@decorator
def greet(name):
print("Hello", name)greet("Khushi")
Output:
Greeting starts
Hello Khushi
Greeting ends
Multiple Decorators
You can apply more than one decorator to a function.
Example:
def deco1(func):
def wrapper():
print("Step 1")
func()
return wrapperdef deco2(func):
def wrapper():
print("Step 2")
func()
return wrapper@deco1
@deco2
def show():
print("Final Function")show()
Here decorators are applied from bottom to top.
Real-Life Example of Decorators
Decorators are widely used in real applications like web development.
For example:
- Checking if a user is logged in
- Measuring execution time
- Logging user activity
Example:
def login_required(func):
def wrapper():
print("Checking login...")
func()
return wrapper@login_required
def dashboard():
print("Welcome to Dashboard")dashboard()
Output:
Checking login...
Welcome to Dashboard
Built-in Decorators in Python
Python also provides built-in decorators:
1. @staticmethod
Used inside classes when function does not use instance variables.
2. @classmethod
Used when function works with class variables.
3. @property
Used to access methods like attributes.
Example:
class Student:
def __init__(self, name):
self.name = name @property
def display(self):
return self.names = Student("Amit")
print(s.display)
Advantages of Decorators
- Code reusability
- Cleaner and readable code
- Separation of concerns
- Easy to extend functionality
Disadvantages of Decorators
- Hard to understand for beginners
- Debugging can be complex
- Too many decorators may reduce readability
When to Use Decorators
Use decorators when:
- You want to add common functionality to multiple functions
- You need logging or authentication
- You want to modify behavior without changing original code
Decorators are a powerful feature in Python that allow you to enhance functions without modifying them directly. They help in writing clean, reusable, and efficient code.
Although they may seem complex at first, once you understand how they work, decorators become one of the most useful tools in Python programming.
By mastering decorators, you step into advanced Python concepts that are widely used in real-world software development, especially in web frameworks like Flask and Django.
business intelligence.
For More Information and Updates, Connect With Us
- Name Sumit singh
- Phone Number: +91 9264477176
- Email ID: emancipationedutech@gmail.com
- Our Platforms:
- Digilearn Cloud
- Live Emancipation
- Follow Us on Social Media:
- Instagram – Emancipation
- Facebook – Emancipation
Stay connected and keep learning with Python Training !