๐ซ Decorators are all you need?
Describing & explaining an important python concept ๐

Hey friends!
Today, let's talk about decorators. No, it's not about hanging wallpaper or painting your walls - though that would be fun too ๐ A decorator in Python is a function that wraps another function, giving it some extra powers without changing the original function itself. Think of it like a cool wrapper that upgrades your function.
Why should you care?
- ๐ Logging - Keep track of who calls your functions and when.
- โฑ Timing - Measure execution time.
- ๐ Access control - Limit who can do what. Useful for web apps and APIs.
- โป๏ธ Caching - Store results of expensive calculations with
functools.lru_cache
. - ๐งน Less repetition - Less spaghetti code. Don't look away, you know what I'm talking about ๐
Quick Example
Here's a tiny decorator that times how long a function takes:
import time
def timer(func):
def wrapper(*args, **kwargs):
start = time.time()
result = func(*args, **kwargs)
end = time.time()
print(f"Function {func.__name__} took {end - start:.4f} seconds")
return result
return wrapper
@timer
def slow_function():
time.sleep(2)
return "done"
print(slow_function())
Run this, and you'll see something like: "Function slow_function took 2.0021 seconds".
Tips
- Decorators can stack: you can wrap a function with multiple decorators for combined effects.
- Use
functools.wraps
in your decorator to preserve the original function's name and docstring. - Decorators aren't just for functions: class decorators exist too, if you want to play at the next level.
In short, decorators are your Python Swiss knife: they make your code cleaner, dryer, and ... hot ๐ฅ