python decorator_memorization_cache.py
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python decorator_memorization_cache.py相关的知识,希望对你有一定的参考价值。
from functools import wraps
def memo(func):
memo = {}
@wraps(func)
def wrapper(*args, **kwds):
key = tuple([tuple(args), tuple(kwds.items())]) # frozen args & kwds.
if key in memo:
print("get result from cache")
else:
print("calculate result")
memo[key] = func(*args, **kwds)
return memo[key]
return wrapper
@memo
def func(name, age, position=None, level=0):
s = f'{name}, {age} years old, position: {position}, result: {level}'
return s
if __name__ == '__main__':
func("L3nvy", 18, position='Software Engineer', level=3)
func("L3nvy", 18, position='Software Engineer', level=3)
func("L3nvy", 18, position='Software Engineer', level=3)
以上是关于python decorator_memorization_cache.py的主要内容,如果未能解决你的问题,请参考以下文章
Python代写,Python作业代写,代写Python,代做Python
Python开发
Python,python,python
Python 介绍
Python学习之认识python
python初识