Created
June 13, 2013 14:37
-
-
Save rafales/5774194 to your computer and use it in GitHub Desktop.
Python recipies
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
class cached_property(object): | |
""" | |
Decorator that converts a method with a single self argument into a | |
property cached on the instance. | |
""" | |
def __init__(self, func): | |
self.func = func | |
def __get__(self, instance, type=None): | |
if instance is None: | |
return self | |
res = instance.__dict__[self.func.__name__] = self.func(instance) | |
return res |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment