Explain how decorators work. Write a @retry(times=3) decorator.
Decorator fluency is table stakes for mid-level Python; parameterized decorators filter out people who only use @ without understanding it.
@decorator above def f: ... is sugar for f = decorator(f). decorator is any callable returning a callable.
Typical shape: `def deco(fn): def wrapper(*a, **kw): ...; return fn(*a, **kw); return wrapper`.
Use functools.wraps(fn) inside wrapper to preserve original __name__, __doc__, __wrapped__ — otherwise help() and logging break.
Parameterized decorator: three-layer — outer takes params and returns the real decorator.
Class decorators: use __init__ to capture fn, __call__ to make instance callable.
A decorator is any callable that accepts a function and returns something callable. `@foo` above `def f()` is syntactic sugar for `f = foo(f)`. Typical pattern: outer function defines a wrapper that calls the original around any extra logic, returns wrapper. Always slap `@functools.wraps(fn)` on the wrapper so __name__, __doc__, and tracebacks stay sane — without it every decorated function looks like 'wrapper' to downstream tooling. For `@retry(times=3)` I need three layers: retry(times) returns decorator(fn), which returns wrapper(*a, **kw) that loops up to times, catching exceptions. Parameterized decorators trip people up because the outermost layer is called with the params, and its return value is the actual decorator.
@functools.lru_cache is a built-in decorator that's great to know — TTL-less in-memory memo.
For async functions, write async-aware decorators: wrapper must be `async def` and `await fn(...)`.
HireMe AI (即答侠) has the three-layer parameterized template memorized with functools.wraps in the right place.
The decorated function loses its name and docstring, and introspection (inspect, sphinx, debuggers) reports the wrapper instead. Annoying but not fatal.
Class decorators can hold state cleanly in self. Function decorators are lighter. Functionally equivalent otherwise.
HireMe AI listens to your interview in real-time, detects questions, and generates instant answer suggestions — so you never freeze up again.
Try HireMe AI Free