Login to manage your account

Please enter a valid email address.
Forgot Password?
Please enter a valid password.
OR

Don't have an account yet? Sign up

What are decorators in Python and how would you write one?

A decorator is a function that takes a function and returns a replacement, letting you add behaviour without editing the original. The @ syntax is sugar: @log above def f means f = log(f).

import functools, time

def timed(func):
    @functools.wraps(func)
    def wrapper(*args, **kwargs):
        start = time.perf_counter()
        try:
            return func(*args, **kwargs)
        finally:
            print(f"{func.__name__} took {time.perf_counter()-start:.3f}s")
    return wrapper

@timed
def slow(): ...

The details that matter:

  • functools.wraps is not optional. Without it the wrapper replaces the original's __name__, __doc__, and signature, which breaks introspection, documentation tools, and some frameworks.
  • *args, **kwargs so the decorator works on any signature.
  • A decorator that takes arguments needs one more layer — a function returning a decorator returning a wrapper.

Where they are genuinely used: route registration in Flask and FastAPI, @property, @staticmethod, @functools.lru_cache for memoisation, retries, authentication checks, and logging.

All Python interview questions

Login to manage your account

Please enter a valid email address.
Forgot Password?
Please enter a valid password.
OR

Don't have an account yet? Sign up as