Summary of small knowledge points of Python system (continuous update...)

1. The functools module

import functools
@functools.lru_cache(None)

#Official example 

@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

Its function is to be used as a cache, which can save relatively time-consuming function results, avoiding repeated calculations by passing in the same parameters, and the cache will not grow indefinitely, and the unused cache will be released. 

When maxsize is set to None, the LRU feature is disabled, and the cache can grow indefinitely.

 

Guess you like

Origin blog.csdn.net/weixin_37724529/article/details/114578571