Created
July 18, 2013 10:27
-
-
Save rob-b/6028309 to your computer and use it in GitHub Desktop.
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 update_wrapper | |
| def cached_property(f, name=None): | |
| if not name: | |
| name = f.__name__ | |
| prop = '_%s' % name | |
| def _get_property(self): | |
| try: | |
| value = getattr(self, prop) | |
| except AttributeError: | |
| value = f(self) | |
| setattr(self, prop, value) | |
| return value | |
| update_wrapper(_get_property, f) | |
| def _del_property(self): | |
| delattr(self, prop) | |
| return property(_get_property, None, _del_property) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Use thusly: