Artur Keraz’s Post

I used to copy-paste the same logging block into every function. Then I learned decorators. And deleted 200 lines of code in one afternoon. Here's what changed: **Before:** ```python def get_user(user_id):   logger.info(f"Calling get_user with {user_id}")   start = time.time()   result = db.query(...)   logger.info(f"get_user took {time.time() - start:.2f}s")   return result ``` Every. Single. Function. 😅 **After:** ```python def log_execution(func):   @wraps(func)   def wrapper(*args, **kwargs):     start = time.time()     result = func(*args, **kwargs)     logger.info(f"{func.__name__} took {time.time() - start:.2f}s")     return result   return wrapper @log_execution def get_user(user_id):   return db.query(...) ``` Clean. Reusable. Readable. The 3 decorator patterns I use every week: - `@log_execution` — timing and logging - `@retry(max_attempts=3)` — auto-retry on transient errors - `@cache_result(ttl=300)` — simple in-memory caching The key insight: decorators are just functions that wrap functions. Once that clicks, you stop copy-pasting and start composing. What's your favorite decorator pattern? Drop it in the comments 👇 #Python #BackendDevelopment #SoftwareEngineering #Programming #Django

To view or add a comment, sign in

Explore content categories