1、functools模块
import functools
@functools.lru_cache(None)
#官方例子
@cache
def factorial(n):
return n * factorial(n-1) if n else 1
>>> factorial(10) # no previously cached result, makes 11 recursive calls
3628800
>>> factorial(5) # just looks up cached value result
120
>>> factorial(12) # makes two new recursive calls, the other 10 are cached
479001600
其作用是用来做缓存,能把相对耗时的函数结果进行保存,避免传入相同的参数重复计算,同时缓存不会无限增长,不用的缓存会被释放。
maxsize设置为None时,LRU特性被禁用,缓存可以无限制地增长。