Remember that there is also the concept of property in cocoa, the property in Python and the property in cocoa seems to be similar. Here are two ways to use it.
Usage One:
classTest (object):def __init__(self):Print "Init is calling"SELF.A=100defGetX (self):Print "Call GetX" returnSELF.AdefSetX (self,value):Print "Call SetX"SELF.A=value x=Property (GETX,SETX)defMain (): a=Test ()PrintA.x#100a.x=150PrintA.x# Maxif __name__=='__main__': Main ()
Getx and Setx These two function names can be arbitrarily taken. There's also a Del method, which is less commonly used, so it's not written here.
Usage Two:
classTest (object):def __init__(self):Print "Init is calling"SELF.A=100@propertydefx (self):Print "Call GetX" returnSELF.A @x.setterdefx (self,value):Print "Call SetX"SELF.A=valuedefMain (): T=Test ()PrintT.x# -t.x=150PrintT.x# Maxif __name__=='__main__': Main ()
Notice that the function name is changed to X.
Use of property in Python