Skip to content

Instantly share code, notes, and snippets.

@zopyx
Created August 14, 2017 07:36
Show Gist options
  • Save zopyx/3fe289fc53ce552c296e8ad52ccb91a2 to your computer and use it in GitHub Desktop.
Save zopyx/3fe289fc53ce552c296e8ad52ccb91a2 to your computer and use it in GitHub Desktop.
def memoize(function):
def wrapper(*args, **kwargs):
key = (args, frozenset(kwargs.items()))
instance = args[0]
if not hasattr(instance, '__cache'):
instance.__cache = {}
if key not in instance.__cache:
instance.__cache[key] = function(*args, **kwargs)
return instance.__cache[key]
return wrapper
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment