Move to single file. Switch to py.test
[cached-property.git] / cached_property.py
1 # -*- coding: utf-8 -*-
2
3 __author__ = 'Daniel Greenfeld'
4 __email__ = 'pydanny@gmail.com'
5 __version__ = '0.1.0'
6
7
8 class cached_property(object):
9 """ A property that is only computed once per instance and then replaces
10 itself with an ordinary attribute. Deleting the attribute resets the
11 property. """
12
13 def __init__(self, func):
14 self.__doc__ = getattr(func, '__doc__')
15 self.func = func
16
17 def __get__(self, obj, cls):
18 if obj is None:
19 return self
20 value = obj.__dict__[self.func.__name__] = self.func(obj)
21 return value