← Python
#6 most askedVery commonMediumDecorators & context managers

Write a decorator

🏒 Asked in real interviews
No reports yet. Were you asked this? Be the first to say where.
I was asked this

Write a decorator count_calls that counts how many times the decorated function is called, stored as wrapper.calls. The function must still return its normal result and keep its name (use functools.wraps).

πŸ§ͺ Practice

Write your solution, then press Run tests. Explain your approach out loud as you go.

Python
import functools

def count_calls(fn):
    pass

Output appears here. Press Run or Ctrl/⌘ + Enter.

Try it first. Struggling for a few minutes is how it sticks.