Back to Practice
#0070
Count Calls With a Decorator
MediumPython25 min20 XP
Problem
Implement count_calls so decorating a function adds a calls attribute that increments each time the function runs.
Why This Matters
Decorators power logging, timing, caching, retries, validation, and web framework routes.
Function Signature
def count_calls(func):
Examples
Example 1
Input@count_calls def add(a,b): return a+b; add(1,2); add(3,4)
Outputadd.calls == 2
The wrapper increments before delegating to the original function.
Constraints
- Support positional and keyword arguments.
- Expose a calls attribute on the returned function.
- Return the original function result.
CodePython
Visible browser tests run here when available.
Testcases1 visible / 3 hidden categories
Counts repeated calls
Inputdecorated add function
Expectedcalls == 2
Each invocation should increment the counter.
Hidden Test Categories
zero calls initiallyfunction with no argumentsreturn value preserved