I have the below code
def memo(fn):
cache = {}
miss = object()
print 'MEMO'
def wrapper(*args):
result = cache.get(args, miss)
print 'IT CALLS'
if result is miss:
print 'IT MISSES'
result = fn(*args)
cache[args] = result
return result
return wrapper
@memo
def fib(n):
if n < 2:
return n
return fib(n - 1) + fib(n - 2)
when I call fib(4) it prints MEMO only once. Following is the output.
MEMO
IT CALLS
IT MISSES
IT CALLS
IT MISSES
IT CALLS
IT MISSES
IT CALLS
IT MISSES
IT CALLS
IT MISSES
IT CALLS
IT CALLS
What is causing this behaviour ??