python - AttributeError:“cached_property”对象没有属性“setter”
问题描述
我正在使用 functools.cached_property 来记忆一些功能。
但它似乎不支持二传手。
例如
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 允许写入。”
推荐阅读
- javascript - 在 Dropify 中查找和更改云图标
- ojalgo - 为什么 Double 不浮动
- php - 始终使用正则表达式在两个字符之间添加空格
- javascript - React - 将函数传递两扇门,带参数
- python - 使用字典中的项目访问字典
- vue.js - 如何创建从 vue.js 实例到自定义原生 Web 组件的双向绑定?
- javascript - 如何在 javascript 搜索功能中传递活动选项卡值?
- mysql - mysql查询,非活跃公司
- c# - System.Web.Helpers 在调试时消失
- javascript - React 在 this.props.children 上设置默认道具