Created
January 17, 2022 16:57
-
-
Save qharlie/bc5ddbc063508cbd5a6031e6595bbe41 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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