Changes to reflect who deserves what credit. #1
[cached-property.git] / cached_property.py
1 # -*- coding: utf-8 -*-
2
3 __author__ = 'Daniel Greenfeld'
4 __email__ = 'pydanny@gmail.com'
5 __version__ = '0.1.4'
6 __license__ = 'BSD'
7
8
9 class cached_property(object):
10 """ A property that is only computed once per instance and then replaces
11 itself with an ordinary attribute. Deleting the attribute resets the
12 property.
13
14 Source: https://github.com/bottlepy/bottle/commit/fa7733e075da0d790d809aa3d2f53071897e6f76
15 """
16
17 def __init__(self, func):
18 self.__doc__ = getattr(func, '__doc__')
19 self.func = func
20
21 def __get__(self, obj, cls):
22 if obj is None:
23 return self
24 value = obj.__dict__[self.func.__name__] = self.func(obj)
25 return value