Decorators Introduction in Python
Introduction
Decorators are a powerful feature in Python that allow you to modify the behavior of functions or classes.
They help you write cleaner and more reusable code by wrapping another function and extending its behavior without explicitly modifying it.
In Python, decorators provide a simple syntax for calling higher-order functions.
What Are Decorators?
A decorator is a function that takes another function as an argument and returns a new function with added functionality.
They are often used to add features like logging, access control, or timing to existing functions.
- Decorators wrap a function to extend its behavior.
- They do not modify the original function's code.
- They are applied using the @decorator_name syntax above a function definition.
How Decorators Work
When you decorate a function, Python passes the original function to the decorator and replaces it with the returned function.
This allows the decorator to execute code before and after the original function runs.
- The original function is passed as an argument to the decorator.
- The decorator returns a wrapper function.
- The wrapper function calls the original function and can add extra behavior.
Basic Decorator Example
Let's look at a simple decorator that prints messages before and after a function runs.
Examples
def my_decorator(func):
def wrapper():
print('Before the function runs')
func()
print('After the function runs')
return wrapper
@my_decorator
def say_hello():
print('Hello!')
say_hello()This example defines a decorator 'my_decorator' that wraps the 'say_hello' function to print messages before and after it runs.
Best Practices
- Use functools.wraps to preserve the original function's metadata.
- Keep decorators simple and focused on a single responsibility.
- Test decorated functions to ensure behavior is as expected.
Common Mistakes
- Forgetting to return the wrapper function from the decorator.
- Not using functools.wraps, which causes loss of function metadata.
- Applying decorators incorrectly without the @ syntax or manual assignment.
Hands-on Exercise
Create a Timing Decorator
Write a decorator that measures and prints the execution time of a function.
Expected output: Printed execution time after the decorated function runs.
Hint: Use the time module and record time before and after the function call.
Interview Questions
What is a decorator in Python?
InterviewA decorator is a function that takes another function and extends or modifies its behavior without changing its source code.
How do you apply a decorator to a function?
InterviewYou apply a decorator by placing @decorator_name above the function definition.
Summary
Decorators are a key Python feature that allow you to modify or extend function behavior cleanly.
They work by wrapping functions and can be applied easily using the @ syntax.
Understanding decorators helps you write more modular and maintainable Python code.
FAQ
Can decorators be applied to classes?
Yes, decorators can also be applied to classes to modify or enhance their behavior.
What is functools.wraps and why is it important?
functools.wraps is a decorator that preserves the original function's metadata when writing decorators.
