Loading…
A short voice session: one question, one follow-up, then instant feedback — up to 5 minutes.
Write a factory function that takes the arguments and returns the actual decorator, which returns the wrapper; functools.wraps copies the wrapped function's name, docstring and other metadata onto the wrapper.
@retry(times=3) first calls retry(times=3), so retry must return a decorator, which then receives the function and returns a wrapper. That makes three levels of nesting: retry(times, delay) returns decorator(func), which returns wrapper(*args, **kwargs) containing the retry loop. The wrapper should catch only the exceptions worth retrying, sleep between attempts and re-raise after the final failure. Without functools.wraps, the decorated function reports the wrapper's name and loses its docstring, which confuses logs, debuggers, documentation and frameworks that inspect signatures. Async functions need an async wrapper that awaits the call.