Decorators with arguments

@repeat(3) looks like a decorator with an argument, but read it carefully: Python first calls repeat(3), and the result is the decorator. So you need three levels:

  1. repeat(times) takes the settings and returns a decorator.
  2. decorator(func) takes the function and returns a wrapper.
  3. wrapper(*args, **kwargs) runs on every call.

Each level is a closure over the one outside it, so wrapper can see both times and func.

A common mistake is writing @repeat without the parentheses. Then repeat receives the function as times, and things break in confusing ways.

Real examples you will meet: @lru_cache(maxsize=128), @pytest.mark.parametrize(...), and web routes like @app.get("/users").

Run it, then change @repeat(3) to @repeat(1).