I am learning how to use decorator in Python. Here I use two different decorator to a function.
Why is the attribute call_count
from the first decorator lost, after I use the second decorator?
Does it still exist in memory, but I can’t find a way to access it?
def fib(n):
if n == 0:
return 0
elif n == 1:
return 1
else:
return fib(n - 2) + fib(n - 1)
def count(f):
def counted(n):
counted.call_count += 1
return f(n)
counted.call_count = 0
return counted
def count_frames(f):
def counted(n):
counted.open_count += 1
counted.max_count = max(counted.max_count, counted.open_count)
result = f(n)
counted.open_count -= 1
return result
counted.open_count = 0
counted.max_count = 0
return counted
fib = count(fib)
fib = count_frames(fib)
print(fib(19), fib.__dict__)
I thought fib.__dict__
would contain the attribute call_count
, but it only contains the attributes open_count
and max_count
from the second decorator
1