Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

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 as long as the descriptor does not define `__set__` method.

Here is an implementation of `cached_property` I use:

    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


Don't do this. Your implementation does not work if someone invokes the dscriptor's __get__ by hand which is not uncommon. My implementation takes the shortcut but also still does the correct thing if you keep a reference to the property object.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: