def cache_function(f):
cache = {}
def f_opt(*args):
key = tuple(map(repr, args))
if key not in cache:
cache[key] = f(*args)
return cache[key]
return f_opt
@cache_function
def expensive_function(x):
return x * x