Skip to content

Instantly share code, notes, and snippets.

@exit99
Created September 4, 2015 14:28
Show Gist options
  • Save exit99/46abbef2b278e78f72b0 to your computer and use it in GitHub Desktop.
Save exit99/46abbef2b278e78f72b0 to your computer and use it in GitHub Desktop.
Instance cache for classes python
def instance_cached(meth):
cached_name = '_' + meth.__name__
@functools.wraps(meth)
def _cached(self):
if not hasattr(self, cached_name):
setattr(self, cached_name, meth(self))
return getattr(self, cached_name)
return _cached
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment