- property([fget[, fset[, fdel[, doc]]]])
- class C(object):
- def __init__(self):
- self._x = None
- def getx(self):
- return self._x
- def setx(self, value):
- self._x = value
- def delx(self):
- del self._x
- x = property(getx, setx, delx, doc="I'm the 'x' property.")
If then c is an instance of C, c.x will invoke the getter, c.x = value will invoke the setter and del c.x the deleter.
If given, doc will be the docstring of the property attribute.
其实这样看来 property 就是,赋值和读取这2个函数以及删除 集合在一个object里,在实际的代码中,不用显示的写这几个函数,而是通过语句含义隐示的调用这几个函数。
http://docs.python.org/2/library/functions.html
转载于:https://blog.51cto.com/dailyc/1063994