Skip to content

Instantly share code, notes, and snippets.

@tk0miya
Created September 4, 2014 04:25
Show Gist options
  • Save tk0miya/1bd5b7623f77115923ff to your computer and use it in GitHub Desktop.
Save tk0miya/1bd5b7623f77115923ff to your computer and use it in GitHub Desktop.
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