Код: Выделить всё
import functools
def memoize(func):
cache = {} # when does this get created, how many different instances?
@functools.wraps(func) # Preserve function metadata
def wrapper(*args, **kwargs):
key = (args, frozenset(kwargs.items())) # Create a unique key for the arguments
if key not in cache:
cache[key] = func(*args, **kwargs)
return cache[key]
return wrapper
Подробнее здесь: https://stackoverflow.com/questions/793 ... -in-python