NameError爲什麼會這樣:使用裝飾產生一個定義的函數
def fn(proc, *args, **kwargs):
cache = proc.cache = {}
def cached_execution(cache, *args, **kwargs):
if proc in cache:
if args in cache[proc]:
return cache[proc][args]
res = proc(args)
cache[proc] = {args: res}
return res
return cached_execution(cache, proc, *args, **kwargs)
@fn
def cached_fibo(n):
if n == 1 or n == 0:
return n
else:
return cached_fibo(n-1) + cached_fibo(n-2)
print cached_fibo(100)
拋出這樣一個異常:
NameError: global name 'cached_fibo' is not defined
我失去了什麼根本性的概念?
(概念上,**kwargs
僅供裝飾使用,不用於檢索緩存結果,但不用擔心)。
你期待的代碼是什麼? –