#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.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.
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.