|
|
|
|
|
by xi
4999 days ago
|
|
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
|
|