Created
October 6, 2011 18:26
-
-
Save whitmo/1268196 to your computer and use it in GitHub Desktop.
caching property
This file contains hidden or 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
| from functools import partial | |
| class CachingDescriptor(object): | |
| def __init__(self, func): | |
| self.cache = None | |
| self.func = func | |
| def __get__(self, obj, objtype): | |
| if self.cache is None: | |
| self.cache = self.func(obj) | |
| return self.cache | |
| def __del__(self, obj): | |
| self.cache = None | |
| class Test(object): | |
| @CachingDescriptor | |
| def big(self): | |
| return ["<%s>" % ('-'*x) for x in range(100)] | |
Author
Author
removed all name stuff as irrelevant
Author
ok... this is sensible and tested
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
with_name is sort of silly, since the reference is contained inside the instantiated descriptor.