Created
January 23, 2015 15:19
-
-
Save itkr/4ca1d1070a6fa87ba6c7 to your computer and use it in GitHub Desktop.
cached_property
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 cached_property(func): | |
@property | |
def f(self): | |
name = "_{}_cache".format(func.__name__) | |
if not hasattr(self, name): | |
setattr(self, name, func(self)) | |
return getattr(self, name) | |
return f |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment