A Wrapper That Counts Itself

Medium ⏱ 12 min 63% acceptance ★★★★★ 4.8
Write counted(func) so the decorated function carries its own call tally as an attribute: after three calls, f.calls == 3. Functions are objects — the wrapper can hang state directly on itself.

Examples

Example 1
Input
f(); f(); f.calls
Output
2
Explanation

The attribute lives on the wrapper function object.

Constraints

  • Expose .calls on the decorated function.
  • Initialize to 0 at decoration time.

Topics

Decoratorsfunction attributes

Companies

AmazonFlipkartSwiggy

Hints

Hint 1

wrapper.calls = 0 after defining wrapper.

Hint 2

Increment inside before delegating.

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.