A typical implementation of a decorator used as @retry(times=3) needs how many nested callables?
Options
- 1
- 2
- 3 (Correct option)
- 4
Answer
3
Why
@retry(times=3) first CALLS retry(3). That call must return a decorator.
The decorator receives your function and returns a wrapper.
The wrapper runs every time your function is called.
So there are three levels: retry (settings) → decorator (function) → wrapper (call arguments).
def retry(times):
def decorator(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
for attempt in range(times):
try:
return func(*args, **kwargs)
except Exception:
if attempt == times - 1:
raise
return wrapper
return decoratorRemember
A decorator with arguments is a function that returns a decorator.