Created
August 14, 2017 07:36
-
-
Save zopyx/3fe289fc53ce552c296e8ad52ccb91a2 to your computer and use it in GitHub Desktop.
This file contains hidden or 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(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