The property in Python is more useful.
First, a code.
#-*-coding:utf-8-*-class C (object): Status_dict = {1: ' Accept ', 2: ' Reject '}def __init__ (self): self._x = 1@propertydef St ATUs (self): return self.status_dict[self._x] @status. Setterdef status (Self, val): if val = = ' Reject ': self._x = 2elif val = = ' Accept ': self._x = 1c = C () print c.statusprint c._xc.status = ' reject ' Print c.statusprint c._x
This code with the property decorator also used the setter, I used to use more property, such as I have developed a state, but want to get this state corresponding to the Chinese description, plus a properties, so you can use this state as the object's attributes to operate, But I know the description of the state want to change the description, what to do, and I do not know the corresponding relationship, this time the setter to play.
-------------------
Cut first, write so much first, and then slowly Add.
[Python learning] Describes the property of Python, and why to use a setter, a little chestnut