easy +10 pts

Count calls decorator

Build a decorator that tracks how many times a function is called.

Write a decorator named `count_calls` that wraps a function and keeps track of how many times it has been called. The decorator should return a new function that behaves exactly like the original function (same arguments, same return value) but also exposes an attribute `call_count` (an integer) that is incremented by 1 on every call. Your task is to implement the decorator so that the following behavior is achieved: - The wrapped function accepts any positional and keyword arguments and returns the same result as the original. - The wrapped function has an attribute `call_count` that starts at 0 and increments by 1 each time the wrapped function is called. - The attribute must be accessible on the wrapped function object (e.g., `wrapped.call_count`). You do not need to use `functools.wraps` for this challenge. Implement the decorator in the file `solution.py`. The tests will apply the decorator to various functions and check both the return values and the `call_count` attribute. For example, the test suite will create functions named `add` and `sum_three` decorated with `count_calls` and check their return values and the `call_count` attribute.

Constraints

The decorated function must accept any combination of positional and keyword arguments. The function may be called multiple times. The total number of calls will not exceed 10^6.

Example

>>> @count_calls
... def add(a, b):
...     return a + b
>>> add(2, 3)
5
>>> add.call_count
1
>>> add(10, 20)
30
>>> add.call_count
2

>>> @count_calls
... def sum_three(a, b, c):
...     return a + b + c
>>> sum_three(1, 2, 3)
6
>>> sum_three.call_count
1
10 points ~15 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Use a mutable variable (like a list or a nonlocal integer) to hold the count.
Inside the wrapper, increment the count before calling the original function.
Attach the count to the wrapper function using an attribute.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.