๐Ÿซ Decorators are all you need?

Describing & explaining an important python concept ๐Ÿ

Decorators Illustration

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 ๐Ÿ”ฅ

Published on August 22, 2025 Author: Vitaly