...

/

Solution: Track Function Call Count with a Decorator

Solution: Track Function Call Count with a Decorator

Wrap a function with a stateful decorator that counts and logs how many times it’s been called.

We'll cover the following...

Solution explanation

  • Lines 2–4: We define multiply, a pure function that returns the product of two numbers. It stays unmodified—our decorator will add behavior externally.

  • Lines 7–14: We build withCallCount(fn), a stateful decorator:

    • We declare a local variable count initialized to 0.

    • Every time the wrapped function runs, we ...