the code below counts the num times the decorated function func was called:
from functools import wraps
def counting_calls(func):
@wraps(func)
def inner(*args, **kwargs):
inner.call_count += 1
return func(*args, **kwargs)
inner.call_count = 0
return inner
for this test:
@counting_calls
def add(a: int, b: int) -> int:
'''return sum of 2 ints'''
return a + b
print(add(10, b=20))
print(add(30, 5))
print(add(3, 5))
print(add(4, 5))
print('num calls =', add.call_count)
print(add(11, 5))
print('num calls =', add.call_count)
why do we reset inner.call_count = 0? does not it actually reset the call_count always to 0 and does not save the total num calls?