When assigning a value to a property, the way in which it is used 实例.属性=属性值
obviously exposes the attribute, and it is impossible to check the property values, and the setter and getter methods are provided in Java, so how does python do it? For more information, see: Python Learning Guide
Property Assignment Method
When binding properties, if we directly expose the properties, although it is very simple to write, but there is no way to check the parameters, resulting in the result can be changed casually:
==9999
The above assignment is obviously not in accordance with the actual situation, in order to limit the scope of the score, you can use a set_score()
method to set the score, and then through one get_score()
to obtain results, so that in the set_score()
method, you can check the parameters:
class Student (object ): def get_score (self ): return se LF . _score def set_score (self , value): if not isinstance (value, int ): raise valueerror ( ' score must is an integer! ' ) if value < 0 or Value > 100 : raise ValueError ( ' score must between 0 ~ 100! ' ) self . _score = value
Now, to operate on any of the student instances, you cannot set score as you wish:
>>>= Student()>>>s.set_score(60)>>>s.get_score()60>>>s.set_score(9999)Traceback (most recent call last): ...ValueError0~100!
However, the above method of invocation is slightly more complex, and it is not straightforward to use attributes directly.
Is there any way to access the variables of a class in such an easy manner as to check parameters and use similar properties? For the perfect Python programmer, this is necessary!
Remember the adorner (Secorator) can add functionality to a function? For a class method, the adorner acts as it does. Python's built-in @property
decorator is responsible for turning a method into a property invocation:
classStudent (Object):@property defScore ( Self):return Self. __score@score. Setter defScore ( Self, value):if not isinstance(Value,int):Raise ValueError(' score must is an integer!!! ')ifValue< 0 orValue> -:Raise ValueError(' score must between 0~100!!! ') Self. __score=Score
@property
Implementation is more complex, we first examine how to use. To turn a getter method into a property, just add it, and @property
at this point, it @property
creates another adorner @score.setter
, which is responsible for turning a setter method into a property assignment, so we have a controllable property operation:
>>>= Student()>>>=60# OK,实际转化为s.set_score(60)>>># OK,实际转化为s.get_score()60>>>=9999Traceback (most recent call last): ...ValueError0~100!
Notice that this magical @property
, we are all in the operation of the instance, we know that the property is probably not directly exposed, but through getter and setter method to achieve.
You can also define read-only properties, define getter methods only, and do not define setter methods as a read-only property:
class Student(object): @property def birth(self): returnself._birth @birth.setter def birth(self, value): self= value @property def age(self): return2015-self._birth
The above birth
is a read-write property, which age
is a readonly property because age
it can be calculated based on the birth
current time.
Summary
@property
Widely used in the definition of a class, you can let the caller write short code, while ensuring that the parameters are necessary to check, so that the program runs to reduce the likelihood of errors.
Python_getter and Setter methods