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)