Decorators
Hacker’s Cloak of Enhancement
Imagine you’re a hacker with a basic tool. It works fine, but you want to add extra abilities like logging every use, checking permissions, or timing its execution without rewriting the tool itself. That’s what decorators do in Python: they wrap functions with additional functionality, like cloaking them with hidden powers.
Decorators let you extend behavior without touching the original code. They’re the hacker’s way of upgrading tools on the fly.
Why Decorators Matter
- Definition: A decorator is a function that takes another function as input and returns a new function with enhanced behavior.
- Reusability: Add features without duplicating code.
- Separation of Concerns: Keep core logic clean while handling extra tasks externally.
- Meta‑Programming: Functions modifying other functions that is Python’s way of hacking its own behavior.
- Real‑World Analogy: Think of a coffee cup sleeve. The cup (function) stays the same, but the sleeve (decorator) adds insulation and grip.
Basic Decorator Example
def decorator(func):
def wrapper():
print("Before function call")
func()
print("After function call")
return wrapper
@decorator
def greet():
print("Hello, Hacker!")
greet()
- Why? The decorator adds behavior before and after the original function.
Output:
Before function call
Hello, Hacker!
After function call
Decorators with Arguments
def repeat(n):
def decorator(func):
def wrapper(*args, **kwargs):
for _ in range(n):
func(*args, **kwargs)
return wrapper
return decorator
@repeat(3)
def hack():
print("Executing hack...")
hack()
- Why? Decorators can be parameterized, making them flexible and reusable.
Practical Decorators
Access Control:
def require_admin(func):
def wrapper(user):
if user == "admin":
return func(user)
else:
print("Access denied.")
return wrapper
@require_admin
def delete_database(user):
print(f"Database deleted by {user}.")
delete_database("admin")
delete_database("guest")
Timing:
import time
def timer(func):
def wrapper(*args, **kwargs):
start = time.time()
result = func(*args, **kwargs)
end = time.time()
print(f"Execution time: {end - start:.4f}s")
return result
return wrapper
@timer
def process():
time.sleep(1)
print("Processing complete.")
process()
Logging:
def log(func):
def wrapper(*args, **kwargs):
print(f"Calling {func.__name__}")
return func(*args, **kwargs)
return wrapper
@log
def connect():
print("Connected to server.")
connect()
Stacking Multiple Decorators
@log
@timer
def hack():
print("Running exploit...")
hack()
- Why? Multiple decorators can be layered, combining enhancements seamlessly.
The Hacker’s Notebook
- Decorators wrap functions, adding behavior without changing original code. They’re reusable, flexible, and perfect for logging, timing, or access control.
- Parameterized decorators adapt to different scenarios. Multiple decorators can be stacked for layered enhancements.
Hacker’s Mindset: treat decorators as cloaks of power. They let you upgrade tools invisibly, keeping your code clean while adding hidden strength.

Updated on Jan 3, 2026