Describes the usage of the @ property modifier in Python
This article mainly introduces the usage of @ property decorators in Python. It is an important knowledge in Python learning. The Code is based on Python2.x. For more information, see
When binding an attribute, If we expose the attribute directly, although it is easy to write, but we cannot check the parameter, so we can change the score as needed:
?
1 2 |
S = Student () S. Scores = 9999 |
This is obviously not logical. To limit the score range, you can use the set_score () method to set the score and get_score () to obtain the score. In this way, in the set_score () method, you can check the parameters:
?
1 2 3 4 5 6 7 8 9 10 11 |
Class Student (object ): Def get_score (self ): Return self. _ score Def set_score (self, value ): If not isinstance (value, int ): Raise ValueError ('score must be an integer! ') If value <0 or value> 100: Raise ValueError ('score must between 0 ~ 100! ') Self. _ score = value |
Now, if you operate any Student instance, you cannot set the score as you like:
?
1 2 3 4 5 6 7 8 |
>>> S = Student () >>> S. set_score (60) # OK! >>> S. get_score () 60 >>> S. set_score (9999) Traceback (most recent call last ): ... ValueError: score must between 0 ~ 100! |
However, the above call method is a little complicated, and it is not as simple as directly using attributes.
Is there a simple way to check parameters and use variables similar to attributes? For a Python programmer who pursues perfection, this must be done!
Do you still remember that the decorator Can add functions to the function dynamically? For Class methods, the decorator works the same way. The @ property modifier built in Python is responsible for calling a method into a property:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 |
Class Student (object ): @ Property Def score (self ): Return self. _ score @ Score. setter Def score (self, value ): If not isinstance (value, int ): Raise ValueError ('score must be an integer! ') If value <0 or value> 100: Raise ValueError ('score must between 0 ~ 100! ') Self. _ score = value |
@ Property implementation is complicated. Let's first look at how to use it. To convert a getter method to an attribute, you only need to add @ property. At this time, @ property itself creates another modifier @ score. setter is responsible for changing a setter method to attribute assignment, so we have a controllable attribute operation:
?
1 2 3 4 5 6 7 8 |
>>> S = Student () >>> S. score = 60 # OK, which is actually converted to s. set_score (60) >>> S. score # OK, which is actually converted to s. get_score () 60 >>> S. score = 9999 Traceback (most recent call last ): ... ValueError: score must between 0 ~ 100! |
Note This magic @ property. When we operate on the instance property, we know that this property is probably not directly exposed, but implemented through the getter and setter methods.
You can also define the read-only attribute. Only the getter method is defined. The setter method is a read-only attribute:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 |
Class Student (object ): @ Property Def birth (self ): Return self. _ birth @ Birth. setter Def birth (self, value ): Self. _ birth = value @ Property Def age (self ): Return 2014-self. _ birth |
The above birth is a read/write attribute, while age is a read-only attribute, because age can be calculated based on birth and the current time.
Summary
@ Property is widely used in class definitions. It allows callers to write short code and check the parameters. This reduces the possibility of errors when running the program.