Skip to content

Instantly share code, notes, and snippets.

@qharlie
Created January 17, 2022 16:57
Show Gist options
  • Save qharlie/bc5ddbc063508cbd5a6031e6595bbe41 to your computer and use it in GitHub Desktop.
Save qharlie/bc5ddbc063508cbd5a6031e6595bbe41 to your computer and use it in GitHub Desktop.
def memoize(f):
""" Memoization decorator for a function taking one or more arguments. """
class memodict(dict):
def __getitem__(self, *key):
return dict.__getitem__(self, key)
def __missing__(self, key):
ret = self[key] = f(*key)
return ret
return memodict().__getitem__
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment