Describes the use of the Python @property adorner

Source: Internet
Author: User
Tags current time

This article mainly introduces the use of Python's @property adorner, is an important knowledge of Python learning advanced, code based on python2.x version, the need for friends can refer to the

When binding properties, if we directly expose the attribute, although it is simple to write, but there is no way to check the parameters, resulting in the results can be arbitrarily changed:


1 2 s = Student () S.score = 9999

This is patently illogical. To limit the range of score, you can use a Set_score () method to set the score, and then a get_score () to get the results, so that 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 isn't isinstance (value, in T): 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 student instance, you can't set the score as you want:


1 2 3 4 5 6 7 8 >>> s = Student () >>> S.set_score # ok! >>> S.get_score >>> S.set_score (9999) Traceback (most recent call last): ... Valueerror:score must between 0 ~ 100!

However, the above method of invocation is slightly more complex, not directly with the attributes so straightforward.

Are there any variables that can be used to check the parameters and to access the class in a simple way such as a property? For the perfect Python programmer, this has to be done!

Remember that the adorner (decorator) can add functionality to the function dynamically? For class methods, the adorner works as well. Python's built-in @property adorner is responsible for turning a method into a property call:


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 Isin Stance (value, int): Raise ValueError (' score must is an integer! ') if value < 0 or value > 100:raise valueerror (' SCO Re must between 0 ~ 100! ') Self._score = value

The implementation of @property is more complicated, we first examine how to use it. To turn a getter method into a property, just add @property, and at this point, @ The property itself creates another adorner @score.setter, which is responsible for turning a setter method into a property assignment, so we have a controllable attribute operation:


1 2 3 4 5 6 7 8 >>> s = Student () >>> s.score = # OK, actually converted to S.set_score >>> S.score # OK, actually converted to S.get_score ( ) >>> S.score = 9999 Traceback (most recent call last): ... Valueerror:score must between 0 ~ 100!

Note that this magical @property, when we operate on an instance attribute, knows that the attribute is probably not directly exposed, but is implemented by getter and setter methods.

You can also define a read-only property, define the getter method only, and do not define a setter method as a read-only property:


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 property, and age is a read-only property, because it can be calculated based on the birth and the current time.


@property widely used in the definition of classes, you can let the caller write short code, while ensuring that the parameters are checked as necessary, so that the program runs to reduce the likelihood of error.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.