我正在尝试实现从数据库返回的值的缓存:
class Foo
...
def getTag(self):
value = self._Db.get(self._f[F_TAG])
setattr(self, 'tag', value)
return value
def _setTag(self, tag):
self._Db.set(self._f[F_TAG], tag)
tag = property(getTag)
...
x = Foo()
x._setTag("20")
print(x.tag)
x._setTag("40")
print(x.tag)
当我第一次处理标签属性时,它必须从数据库中获取值并用实例字段覆盖类字段标签以供后续使用,但出现错误:
Traceback (most recent call last):
File "/home/altera/www/autoblog/core/dbObject.py", line 99, in <module>
print(x.tag)
File "/home/altera/www/autoblog/core/dbObject.py", line 78, in getTag
setattr(self, 'tag', value)
AttributeError: can't set attribute