Introduction to Decorators in Python
Decorators are a powerful and versatile feature in Python that allow you to modify or extend the behavior of functions and methods. They provide a convenient way to add functionality to existing code without modifying its structure. This tutorial will introduce you to the concept of decorators and demonstrate their usage.
What are Decorators?
Decorators in Python are functions that accept another function as input and return a new function as output. They are typically used to modify or wrap the behavior of the input function.
Example:
# Decorator syntax
def my_decorator(func):
def wrapper():
print('Before calling the function')
func()
print('After calling the function')
return wrapper
@my_decorator
def say_hello():
print('Hello, world!')
say_hello()
Output:
Before calling the function
Hello, world!
After calling the function
Common Patterns
Decorators are commonly used for tasks such as logging, authentication, caching, and more. They allow you to separate concerns and keep your code modular and maintainable.
Example:
# Decorator for logging
def log(func):
def wrapper(*args, **kwargs):
print(f'Calling function {func.__name__}')
result = func(*args, **kwargs)
print(f'Finished calling function {func.__name__}')
return result
return wrapper
@log
def add(a, b):
return a + b
print(add(1, 2))
Output:
Calling function add
Finished calling function add
3
By mastering decorators, you can write more modular and reusable code in Python, improving your codebase's maintainability and flexibility.
Related Topics
Related Resources
The Ultimate AI Learning Roadmap for Software Engineers (2025 Edition)
The line between 'software engineer' and 'AI engineer' is disappearing. Are you prepared for the shift from deterministic coding to orchestrating intelligent, probabilistic systems? This comprehensive AI learning roadmap is designed specifically for software professionals. It's a practical, timeline based guide to not only learn the necessary skills but also leverage your existing engineering expertise to transition into a high impact AI role, covering everything from mathematical foundations to production grade MLOps and Generative AI.
articleBuild AI Agents from Scratch with Python and Gemini: A Beginner Friendly Guide to Use Cases and Challenges
AI agents are moving beyond simple chatbots, and with Python and Gemini, beginners can now start building useful autonomous workflows faster than ever. This article introduces AI agents in a practical, beginner friendly way and shows how Python and Gemini can be used to create them from scratch. It covers the core building blocks, a simple development path, real world use cases, and the main challenges to watch for when getting started.
articleList Comprehension in Python
Python is known for its simplicity and readability, and one of the features that truly embodies these qualities is list comprehension. If you’re new to Python, list comprehension might seem a bit intimidating at first, but once you get the hang of it, you’ll see how it can make your code more concise and elegant. In this blog, we’ll explore list comprehension, starting with simple examples and gradually moving on to more complex ones. By the end of this guide, you’ll have a solid understanding of how to use list comprehension in your own projects.