Pythonのプロパティについて
@propertyデコレータをつける。
この場合、読み取り専用のプロパティになる。
class hogehoge(object): def __init__(self): self._x = "****" @property def x(self): return self._x
さらに、@***.setterデコレータをつけると
書き込みが可能なプロパティになる。
class hogehoge(object): def __init__(self): self._x = "****" @property def x(self): return self._x @x.setter def x(self, v): self._x = v
Pythonのインタプリタの動作としては、
classHogeHoge.x = v
が呼ばれた場合、classHogeHogeの _set_x(v)が呼び出される。
z = classHogeHoge.x
が呼ばれると、classHogeHogeの _get_x()が呼び出される。
クラス内部の実装(仮)
def classHogeHoge(object): def __init(self): self._x = 0 def _set_x(self, v): self._x = v def _get_x(self): return self._x