when i execute this piece of code in the decorator the func.count=0 is not assigned and the incrementation is not done. Could anyone help me in solving this problem and make the decorator to retu the func.count and get the correct output as required
def count_calls(skip_recursion=True):
if skip_recursion==True:
def ier(func):
func.count=0
def counter(i):
func.count+=1
retu func.count
retu counter
retu ier
else:
def ier(func):
func.count=0
def counter(i):
func.count+=1
retu func.count
retu counter
retu ier
def test_calls_decorator():
@count_calls()
def fib(n):
if n <= 0:
raise ValueError("n <= 0")
if n == 1 or n == 2:
retu 1
retu fib(n-1) + fib(n-2)
print [fib(i) for i in range(1,6)]
print fib.count # only top calls counted = 5
# with recursion, count all calls, but time only top level calls.
@count_calls(skip_recursion=False)
def fib(n):
if n == 1 or n == 2:
retu 1
retu fib(n-1) + fib(n-2)
print [fib(i) for i in range(1,6)]
print fib.count # all calls counted = 19
test_calls_decorator()
