我对如何在 Python 中使用 @property
感兴趣。我已经阅读了 Python 文档,在我看来,那里的示例只是一个玩具代码:
class C(object):
def __init__(self):
self._x = None
@property
def x(self):
"""I'm the 'x' property."""
return self._x
@x.setter
def x(self, value):
self._x = value
@x.deleter
def x(self):
del self._x
我不知道我可以得到什么好处(s)从包装的 _x
充满了属性装饰。为什么不直接实现:
class C(object):
def __init__(self):
self.x = None
我认为,属性特性在某些情况下可能是有用的。但是什么时候呢?谁能给我一些现实生活中的例子?