AttributeError: 'cached_property' object has no attribute 'setter'

Viewed 2073

I'm using the functools.cached_property to memoize some functions.

but it seems not to be supporting setter.

for example

class User
  @cached_property
  def name(self):
    return self._name

  @name.setter
  def name(self, val):
    self._name = val

but when importing i encountered AttributeError: 'cached_property' object has no attribute 'setter'

1 Answers

@cached_property is designed for immutable values. You cannot expect that this decorator will mimic all features from standard property. In python doc you can find: "The mechanics of cached_property() are somewhat different from property(). A regular property blocks attribute writes unless a setter is defined. In contrast, a cached_property allows writes."

Related