Python Decorators – the very important feature of python which gives precision and exactness to contemporaneity in a strong & modern programming language Python. Decorators might be magical at first, but once you get used to them they are incredibly powerful. This allows you to change or enhance functions and methods without taking the source code into account. If you’ve ever felt like an amateur repeating code, or simply wished to add something extra cool into your function then decorators are about to be come your new best friend.
What is a Decorator?
A decorator is a function that takes another function and it does this without explicitly modifying the code of the original. It is like wrapping a gift, the original function is your beautiful & happy smiley face box and text decorator does wrap it with shiny paper to give extra flavor.
How Do Decorators Work?
A decorator is basically a higher-order function – it takes in a function, and also returns another function that will usually add some functionality to the original one. Let me give you easy example to understand this concept .
def simple_decorator(func):
def wrapper():
print("Something is happening before the function is called.")
func()
print("Something is happening after the function is called.")
return wrapper
@simple_decorator
def say_hello():
print("Hello!")
say_hello()
When you run this code, you’ll see:
Something is happening before the function is called.
Hello!
Something is happening after the function is called.
The @simple_decorator
syntax is just a syntactic sugar. It’s a shorthand for say `hello = simple_decorator(say_hello)
.`
Why Use Decorators?
Decorators are incredibly useful for a variety of reasons:
- Code Reusability: By isolating repetitive code in decorators, you can keep your core functions clean and focused.
- Separation of Concerns: Decorators help in separating the core logic of your functions from auxiliary tasks like logging, access control, and input validation.
- Enhanced Readability: With decorators, you can keep your functions concise, improving readability and maintainability.
Common Use Cases
Let’s explore some common scenarios where decorators shine.
1. Logging
Decorators are great for logging function calls:
def log_decorator(func):
def wrapper(*args, **kwargs):
print(f"Calling function {func.__name__}")
result = func(*args, **kwargs)
print(f"Function {func.__name__} finished")
return result
return wrapper
@log_decorator
def add(a, b):
return a + b
add(2, 3)
Output will be
Calling function add
Function add finished
2. Access Control
They’re also perfect for enforcing access control:
pythonCopy codedef require_admin(func):
def wrapper(user_role):
if user_role == 'admin':
return func(user_role)
else:
print("Access Denied")
return wrapper
@require_admin
def perform_admin_task(user_role):
print("Admin task performed")
perform_admin_task('admin') # Admin task performed
perform_admin_task('guest') # Access Denied
3. Timing Functions
Measuring the execution time of functions can be neatly handled with decorators:
pythonCopy codeimport time
def timer_decorator(func):
def wrapper(*args, **kwargs):
start_time = time.time()
result = func(*args, **kwargs)
end_time = time.time()
print(f"Function {func.__name__} took {end_time - start_time} seconds to complete")
return result
return wrapper
@timer_decorator
def slow_function():
time.sleep(2)
print("Slow function finished")
slow_function()
Output:
vbnetCopy codeSlow function finished
Function slow_function took 2.002 seconds to complete
Nesting Decorators
You can stack multiple decorators on a single function. The decorators are applied from the closest to the farthest:
pythonCopy code@log_decorator
@timer_decorator
def complex_function():
time.sleep(1)
print("Complex function finished")
complex_function()
Output will be:
vbnetCopy codeCalling function wrapper
Complex function finished
Function wrapper took 1.001 seconds to complete
Function wrapper finished
Conclusion
Decorators are a powerful feature in Python that can help you write cleaner, more modular, and more expressive code. They allow you to add functionality to your functions in a reusable way, improving both readability and maintainability.
So the next time you find yourself needing to add some extra functionality to your functions, consider using decorators. They might just be the magic you need to simplify your code and make it more Pythonic.