Do these postings of slides ever help anyone? I never seem to get anything out of just clicking through slides with no explanations. It's essentially reading 20 unjustified bullet points to me.
Armin's implementation of `cached_property` is not entirely correct. Well, it works, but the branch where `value` is not `missing` is never executed: the object's `__dict__` takes precedence over the descriptor <i>as long as the descriptor does not define `__set__` method</i>.<p>Here is an implementation of `cached_property` I use:<p><pre><code> class cached_property(object):
def __init__(self, fget):
self.fget = fget
self.__name__ = fget.__name__
self.__module__ = fget.__module__
self.__doc__ = fget.__doc__
def __get__(self, obj, objtype=None):
if obj is None:
return self
value = self.fget(obj)
# For a non-data descriptor (`__set__` is not defined),
# `__dict__` takes precedence.
obj.__dict__[self.__name__] = value
return value</code></pre>
If you are interested in learning more about python and caching I wrote a post about it couple days ago:<p><a href="http://russell.ballestrini.net/explaining-cache-with-python/" rel="nofollow">http://russell.ballestrini.net/explaining-cache-with-python/</a>