这个函数,好像有 。net的影子
>>> t=C()
>>> t.x
get x
>>> t.x="en"
set x
>>> print t.x
get x
en
>>> del t.x
del x
>>> t.x
class
C(object):
def __init__ (self): self._x = None
def getx(self): print " get x " ; return self._x
def setx(self, value): print " set x " ; self._x = value
def delx(self): print " del x " ; del self._x
x = property(getx, setx, delx, " I'm the 'x' property. " )
使用
def __init__ (self): self._x = None
def getx(self): print " get x " ; return self._x
def setx(self, value): print " set x " ; self._x = value
def delx(self): print " del x " ; del self._x
x = property(getx, setx, delx, " I'm the 'x' property. " )
>>> t=C()
>>> t.x
get x
>>> t.x="en"
set x
>>> print t.x
get x
en
>>> del t.x
del x
>>> t.x
get x
本文转自博客园刘凯毅的博客,原文链接:python property 学习,如需转载请自行联系原博主。