Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the purpose of the @ property attribute in python

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

In this issue, the editor will bring you about the role of @ property attribute in python. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

When binding attributes, if we directly expose the attributes, although it is easy to write, there is no way to check the parameters, resulting in the score can be changed at will:

S = Student () s.score = 9999

This is obviously illogical. To limit the scope of score, you can use a set_score () method to set the score, and then a get_score () to get the score, so that in the set_score () method, you can check the parameters:

Class Student (object): def get_score (self): return self._scoredef set_score (self, value): if not isinstance (value, int): raise ValueError ('score must be an integers') if value

< 0 or value >

100:raise ValueError ('score must between 0 ~ 100') Self._score = value

Now, if you operate on any Student instance, you can't set score at will:

> 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 invocation method is slightly more complex and is not as straightforward as using attributes directly.

Is there a way to check parameters and access class variables in such a simple way as properties? For Python programmers who pursue perfection, this is a must!

Remember that decorator can add functions to functions dynamically? The decorator also works for the methods of the class. Python's built-in @ property decorator is responsible for turning a method into a property call:

Class Student (object): @ propertydef score (self): return self._score@score.setterdef score (self, value): if not isinstance (value, int): raise ValueError ('score must be an integers') if value

< 0 or value >

100:raise ValueError ('score must between 0 ~ 100') Self._score = value

The implementation of @ property is more complex, so let's first examine how to use it. To turn a getter method into an attribute, you only need to add @ property. At this point, @ property itself creates another decorator @ score.setter, which is responsible for turning a setter method into a property assignment, so we have a controllable property operation:

> s = Student () > s.score = 60 # OK, actually converted to s.set_score (60) > s.score # OK, actually converted to s.get_score () 60 > s.score = 9999Traceback (most recent call last):. ValueError: score must between 0-100!

Noticing the magic @ property, we know that when we operate on an instance property, it is likely not to be exposed directly, but through the getter and setter methods.

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

Class Student (object): @ propertydef birth (self): return self._birth@birth.setterdef birth (self, value): self._birth = value@propertydef age (self): return 2014-self._birth

The above birth is a read-write property, while age is a read-only property, because age can be calculated based on birth and the current time.

This is the role of @ property attribute in python shared by the editor. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report