Loading…
A short voice session: one question, one follow-up, then instant feedback — up to 5 minutes.
A decorator is a callable that takes a function and returns a new function, usually a wrapper that adds behaviour before or after calling the original; @timer is shorthand for func = timer(func).
Functions are objects, so they can be passed to and returned from other functions. A timing decorator defines wrapper(*args, **kwargs) that records time.perf_counter(), calls the original function, computes the elapsed time, logs it and returns the original result. The decorator returns wrapper, and @timer above a definition replaces the function with it. Applying functools.wraps(func) to wrapper keeps the original name, docstring and signature for debugging and tools. Common uses include logging, caching, access checks, retries and route registration in web frameworks.