Created
September 4, 2014 04:25
-
-
Save tk0miya/1bd5b7623f77115923ff 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
from functools import wraps | |
def memoize(func): | |
cache = {} | |
@wraps(func) | |
def wrapper(*args, **kwargs): | |
key = args + tuple(kwargs.items()) | |
if key not in cache: | |
cache[key] = func(*args, **kwargs) | |
return cache[key] | |
return wrapper | |
def cached_property(func): | |
@property | |
@wraps(func) | |
def wrapper(self): | |
if not hasattr(self, '__property_cache__'): | |
self.__property_cache__ = {} | |
if func.__name__ not in self.__property_cache__: | |
self.__property_cache__[func.__name__] = func(self) | |
return self.__property_cache__[func.__name__] | |
return wrapper |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment