- """A decorator that converts a function into a lazy property. The
-function wrapped is called the first time to retrieve the result
-and then that calculated result is used the next time you access
-# calculate something important here
-The class has to have a `__dict__` in order for this property to
-the `writeable` attribute and parameter was deprecated. If a
-cached property is writeable or not has to be documented now.
-For performance reasons the implementation does not honor the
-writeable setting and will always make the property writeable.
- # implementation detail: this property is implemented as non-data
- # descriptor. non-data descriptors are only invoked if there is
- # no entry with the same name in the instance's __dict__.
- # this allows us to completely get rid of the access function call
- # overhead. If one choses to invoke __get__ by hand the property
- # will still work as expected because the lookup logic is replicated
- # in __get__ for manual invocation.
- def __init__(self, func, name=None, doc=None, writeable=False):
- from warnings import warn
- warn(DeprecationWarning('the writeable argument to the '
- 'cached property is a noop since 0.6 '
- 'because the property is writeable '
- 'by default for performance reasons'))
- self.__name__ = name or func.__name__
- self.__module__ = func.__module__
- self.__doc__ = doc or func.__doc__
+ def __init__(self, func):
def __get__(self, obj, type=None):
- value = obj.__dict__.get(self.__name__, _missing)
- obj.__dict__[self.__name__] = value
+ obj.__dict__[self.func.__name__] = value = self.func(obj)