Python Decorators: A Practical Guide
Master Python decorators with practical examples for logging, authentication and caching — write cleaner, reusable code.
Master Python decorators with practical examples for logging, authentication, caching, and more.
Introduction
Decorators are one of the most powerful — and most misunderstood — features in Python.
- ✔ Add functionality to existing functions without modifying their source code
- ✔ Implement logging, authentication, caching, rate-limiting, and more
- ✔ Write cleaner, more reusable, more professional code
You can master decorators even as an intermediate Python programmer. This guide explains decorators clearly, with practical examples you can use immediately.
1. What Is a Decorator in Python?
A decorator is simply a function that takes another function and adds extra behaviour to it — without changing the original function's code.
- 🎁 gift = original function
- 🎁 wrapping paper = decorator
- 🎁 wrapped gift = enhanced function
2. Why Use Decorators?
Decorators are used everywhere in professional Python codebases because they help solve common problems:
- ⭐ Logging — Record when a function is called.
- ⭐ Authentication — Check if a user is authorized.
- ⭐ Speed Measurement — Find performance bottlenecks.
- ⭐ Retry Logic — Retry failed API calls.
- ⭐ Validation — Ensure function arguments are correct.
- ⭐ Caching (Memoization) — Return saved results for expensive functions.
If you plan to build web apps, APIs, AI scripts, or automation — decorators will become your favourite tool.
3. Your First Useful Decorator (Logging Example)
Let's build a decorator that logs when a function is executed.
Just like that, you've added logging around the function — without touching its code.
4. Decorators With Arguments
Sometimes, you want the decorator itself to receive extra arguments.
This uses a three-level decorator, a common pattern:
- outer → receives decorator arguments
- middle → receives the function
- inner → runs the function
5. Practical Example: Measuring Function Speed
Performance monitoring is essential in ML, data pipelines, and backend work.
This decorator is used in real-world apps to identify slow functions.
6. Using functools.wraps (Important!)
Without functools.wraps, decorators break metadata:
- function names
- IDE/autocomplete behaviour
7. Real-World Use Case: Authentication Decorator
This is the type of decorator used in Flask, Django, and FastAPI.
If user is not logged in → error. This pattern powers authentication systems across the web.
8. Caching / Memoization Decorator (Performance Boost)
Useful for expensive computations — like ML preprocessing, API calls, Fibonacci, etc.
9. Stacking Multiple Decorators
- Logger wraps inner function
- Timer wraps logger
10. Summary
- ✔ What decorators are
- ✔ Why they're useful
- ✔ How they wrap existing functions
- ✔ How to add arguments to decorators
- ✔ How to log, time, validate, authenticate
- ✔ How to use functools.wraps
- ✔ How real frameworks rely heavily on decorators
- Flask / Django / FastAPI routing
- ML model caching
- Logging systems
- Database middleware
- API request validation
- Authentication layers
Mastering decorators is a major step toward becoming a professional Python developer.
Related articles
- 10 Python Tips Every Beginner Should Know — Discover essential Python tips that will help you write cleaner, more efficient code from day one.
- Object-Oriented Programming in Python — Understanding classes, objects, and inheritance in Python with real-world examples.
- Error Handling Best Practices in Python — Master professional error handling in Python. Learn try/except patterns, logging strategies, custom exceptions, and production-ready techniques for reliable applications.