Skip to content

Instantly share code, notes, and snippets.

@shmatov
Created July 16, 2014 12:32
Show Gist options
  • Save shmatov/772732352f37fcd942c7 to your computer and use it in GitHub Desktop.
Save shmatov/772732352f37fcd942c7 to your computer and use it in GitHub Desktop.
from functools import wraps
def memoize(f):
cache = {}
kwargs_mark = object()
@wraps(f)
def memoized(*args, **kwargs):
key = args + (kwargs_mark,) + tuple(sorted(kwargs.items()))
try:
return cache[key]
except KeyError:
result = cache[key] = f(*args, **kwargs)
return result
return memoized
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment