使用装饰器实现斐波那契数列

一、使用装饰器可以提高重用性,使用缓存可以提高执行效率

二、实现实例:

def outter(func):
cache = {}

def inner(*args):
if cache.get(args):
res = cache.get(args)
return res
res = cache[args] = func(*args)
return res
return inner


@outter
def fibnew(n):
if n in (1, 2):
return 1
return fibnew(n-1) + fibnew(n-2)


res = fibnew(100)
print(res)
posted @ 2021-06-01 10:57  只管去做-王炸  阅读(115)  评论(0)    收藏  举报