データベースから返された値のキャッシュを実装しようとしています:
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)
初めてタグプロパティを処理するときは、DB から値を取得し、次の使用のためにクラスフィールドタグをインスタンスフィールドでオーバーライドする必要がありますが、エラーが発生します。
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