@decorator and Decorator Arguments
Scenario: One retry wrapper needs a configurable attempt count.
New words, in plain English
- @ syntax - short syntax replacing a function with a decorator result
- Decorator factory - a function that receives settings and returns a decorator
- Higher-order function - a function taking or returning functions
@logged means func = logged(func). A configurable decorator adds an outer factory level. Trace the layers slowly: configuration, function, then call arguments.
Analogy: A Python program is like a clear set of instructions for a careful helper: names label things, indentation groups steps, and errors explain where the helper became confused.
A small, real example
def repeat(times):
def decorate(func):
def wrapper(*args, **kwargs):
return [func(*args, **kwargs) for _ in range(times)]
return wrapper
return decorate
@repeat(3)
def ping(): return "pong"
Tip: Prefer a plain helper when decorator nesting makes control flow harder to understand.
Goal: Practice this idea in the py-decorators challenge at/labs/python. Fix the broken program, run it yourself, then usecheckto prove the real end state.