python property

property Found at: __builtin__
property(fget=None, fset=None, fdel=None, doc=None) -> property attribute

fget is a function to be used for getting an attribute value, and likewise
fset is a function for setting, and fdel a function for del'ing, an
attribute. Typical use is to define a managed attribute x:
class C(object):
def getx(self): return self.__x
def setx(self, value): self.__x = value
def delx(self): del self.__x
x = property(getx, setx, delx, "I'm the 'x' property.")

 

class SessionMixin(object):

    def __init__(self):

        self._x = 'init'

        

    def getx(self):

        print 'getx',

        return self._x

    

    def setx(self, value):

        print 'setx'

        self._x = value

        

    def delx(self):

        print 'delx'

        del self._x

    

    x = property(getx, setx, delx, "I'm the 'x' property.")



test = SessionMixin()



test.x = 'value'

print test.x

del test.x

 

输出:

setx
getx value
delx

 

你可能感兴趣的:(property)