Caching - AndrewMZ6/python_cheat_sheet GitHub Wiki
import sys
def cached(func):
cache = {}
def wrapper(x):
if x in cache:
print(f"{x} is in cache! returning cached value")
return cache[x]
else:
cache[x] = func(x)
print(f"adding {x} to cache")
return cache[x]
return wrapper
x = sys.argv[1]
@cached
def func(u):
return u
func(x)
func(x)
CMD
cmd:\>python script.py 5
OUTPUT
adding 5 to cache
5 is in cache! returning cached value