【发布时间】:2015-05-23 04:07:26
【问题描述】:
使用下面带有斐波那契函数的常见记忆模式,我在想象它的实际工作方式时遇到了一些麻烦。我理解这个概念,但在思考程序时我会感到困惑并开始思考。
def memoize(func):
cache={}
def wrapper(*args,**kwargs):
if args in cache:
return cache[args]
else:
cache[args]=func(*args) #to store the result, doesn't fib still have to go through all it's calculations here?
return cache[args]
return wrapper
@memoize
def fib(n):
if n==0:
return 0
elif n==1:
return 1
else:
return fib(n-2)+fib(n-1)
所以从概念上讲,我了解递归是如何工作的,并且我了解记忆化背后的想法(首先尝试查找它以避免多次运行相同的计算)。但是让我感到困惑的是,当您为尚未看到参数的函数调用向字典中添加键时,如何在不通过完整函数调用的情况下完成此操作。
例如,如果我们调用 fib(5),那么当我们点击 cache[args]=func(*args) 时,我们会在字典中添加一对 key:value 和 5:fib(5)(因为还没有看到 5)。但是k:v 对中的v 是对 fib(5) 的函数调用,它仍然需要返回它的值,我认为这需要经历整个递归过程。如果我们尝试缓存返回值,那么memoization如何在不经过整个递归过程的情况下为fib(5)获取这个返回值?
【问题讨论】:
标签: python caching recursion decorator memoization