Created
September 4, 2015 14:28
-
-
Save exit99/46abbef2b278e78f72b0 to your computer and use it in GitHub Desktop.
Instance cache for classes python
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 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