Decorator in Python

A decorator in Python is a function that modifies the behaviour of another function, method, or class, without changing its actual code. It's like putting a filter, wrapper, or enhancer around your function.
Imagine your function is a plain dosa.
The decorator is the masala filling and chutney you wrap around it.
Same dosa, but now it’s "Masala Dosa Deluxe"
The @my_decorator wraps say_hello — without modifying its original logic!
@decorator is syntactic sugar for:
Decorators use closures (a function inside another function) to wrap and enhance behavior.
Use Case | Example Decorator |
Logging |
|
Timing |
|
Authorization |
|
Caching |
|
Input validation |
|
Output:
Decorator Is... | Meaning |
A wrapper | Enhances a function without changing its source |
Reusable | Can be applied to many functions |
Flexible | Can accept arguments |
Readable | Makes cross-cutting concerns (logging, auth, etc.) cleaner |