- Python Snacks
- Posts
- Python Decorators: A Powerful Tool for Code Reusability and Modularity
Python Decorators: A Powerful Tool for Code Reusability and Modularity
A Step-by-Step Approach to Python Decorator Implementation with Examples
If you’ve ever seen the @ character in Python code, you’ve probably seen it used as such:
@add_logging
@auth_check
def user_login(*args, **kwargs):
...
These are what is known as decorators. Python packages such as click leverage decorators to extend functionality into your program.
Let’s take a dive in this tutorial to show you how these work and how you can use them with an example using Flask.
What are Decorators?
Think of a Python function as a bare Christmas tree. It's there, it serves its purpose, but it's a bit plain and ordinary. What if you want to add some extra flair and personality to your tree? That's where decorators come in!
Decorators are like the ornaments, lights, and other decorations you add to a Christmas tree. They allow you to enhance the functionality of a function or a method without modifying its original code.
How Do Decorators Work?
Decorators allow you to run code both before and/or after your function. Here’s a simple example:

Example of a Python Decorator
When we call test_function(), it will first run the sample_wrap() function. In the sample_wrap() function, we have 2 print statements that will print out the code that runs before calling your function and code that runs after your function is ran.
So, if we run this, we’ll see the following output:
>>> test_function()
Code before running function
Code inside of the function
Code after running function
This is the reason why decorators are so powerful - you can change the functionality and behavior of the function without ever touching it.
Example: User Permissions with Flask
Let’s say that you want to set up an authentication system that checks to make sure that the user has the proper roles to perform certain actions in Flask.
We can write a decorator to check these permissions:

Decorator in Python checking user permissions for a Flask application
Then, in our code, we can apply this decorator without adding duplicate code to each of our functions: get_users(), update_user(), and delete_user():

📧 Join the Python Snacks Newsletter! 🐍
Want even more Python-related content that’s useful? Here’s 3 reasons why you should subscribe the Python Snacks newsletter:
Get Ahead in Python with bite-sized Python tips and tricks delivered straight to your inbox, like the one above.
Exclusive Subscriber Perks: Receive a curated selection of up to 6 high-impact Python resources, tips, and exclusive insights with each email.
Get Smarter with Python in under 5 minutes. Your next Python breakthrough could just an email away.
You can unsubscribe at any time.
Interested in starting a newsletter or a blog?
Do you have a wealth of knowledge and insights to share with the world? Starting your own newsletter or blog is an excellent way to establish yourself as an authority in your field, connect with a like-minded community, and open up new opportunities.
If TikTok, Twitter, Facebook, or other social media platforms were to get banned, you’d lose all your followers. This is why you should start a newsletter: you own your audience.
This article may contain affiliate links. Affiliate links come at no cost to you and support the costs of this blog. Should you purchase a product/service from an affiliate link, it will come at no additional cost to you.
Reply