python - 属性错误 : 'cached_property' object has no attribute 'setter'

标签 python

我正在使用 functools.cached_property 来内存一些函数。

但是好像不支持setter。

例如

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

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

但是在导入的时候遇到了AttributeError: 'cached_property' object has no attribute 'setter'

最佳答案

@cached_property 专为不可变值而设计。你不能指望这个装饰器会模仿标准属性的所有特性。在 python 文档中,您可以找到: “cached_property() 的机制与 property() 有所不同。除非定义了 setter,否则常规属性会阻止属性写入。相反,cached_property 允许写入。”

关于python - 属性错误 : 'cached_property' object has no attribute 'setter' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62013695/

相关文章:

python - 字符串格式化和解析

python - 从 Python 中的字符串中提取字段和值

python - 使用 pip 的代理意识

python - 如何通过 AJAX 使用 Flask-WTForms CSRF 保护?

python - 打包 python 应用程序

python - 类型错误 : input expected at most 1 arguments, 得到 3

python - PIL/Pillow 解码 icc 配置文件信息

python - sum() 不适用于 Timedelta 对象列表

python - 验证 PyPI Python 包的完整性

python - python中无替换的加权随机样本