【发布时间】:2011-08-06 19:31:57
【问题描述】:
我有一个使用多个 property() 的类。修改文本的字体、大小或字符串等。将需要重新渲染要缓存的表面。
在 init 中调用类自己的 property() 的推荐方法是什么?问题是变量没有设置,当时我想调用@property DrawText.text
如果我直接设置._text,它会运行:
class DrawText(object):
"""works, Except ignores text.setter"""
def __init__(self):
# self.text = "fails" # would fail if here
self._text = "default"
self.text = "works"
@property
def text(self):
'''plain-text string property'''
return self._text
@text.setter
def text(self, text):
if self._text == text: return
self._text = text
self.dirty = True # .. code re-creates the surface
这也可以运行,并且更接近,但它是否适用于使用不同数据的多个实例?
class DrawText(object):
"""works, Except ignores text.setter"""
def __init__(self):
DrawText.text = "default"
self.text = "works"
@property
def text(self):
'''plain-text string property'''
return self._text
@text.setter
def text(self, text):
if self._text == text: return
self._text = text
self.dirty = True # .. code re-creates the surface
【问题讨论】:
标签: python properties