In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the relevant knowledge of how to use Python @ property, the content is detailed and easy to understand, the operation is simple and quick, and has a certain reference value. I believe you will gain something after reading this article on how to use @ property of Python. Let's take a look.
1. Several concepts
_ a (pre-order underscore), this attribute only indicates that the contract is private, not really private.
_ a (prefixed double underscore), which indicates that it is private and cannot be accessed externally.
_ a _ (double underscore before and after), which identifies the system attribute. (optional)
A _ (back order underscore), this attribute is to avoid conflicts with reserved keywords. (optional)
2. Give an example
Define a class:
Class Student (object): _ sex= "male" _ _ age=0
Execute: (private properties cannot be accessed externally)
> stu = Student () > stu._sex "male" > stu.__ageTraceback (most recent call last): File ", line 1, in AttributeError:" Student "object has no attribute" _ _ age "> 3. Solve the problem.
From the above class, we can see that private properties cannot be accessed in the class instance. What should we do? When we need to query and modify the private attribute _ _ age of the class, we can define get_age and set_age to implement it.
Class Student (object): _ sex= "male" _ _ age=0 def get_age (self): return self.__age def set_age (self,age): self.__age = age
Execute:
> stu = Student () > stu.get_age () 0 > > stu.set_age (18) > stu.get_age () 18 > > 4. Change the method
But the above approach is a little complicated and not suitable for classes with more private attributes, so we expect to find a simpler way to solve this problem, such as converting this private property to another property. To tell you the good news, Python has done it for us, and this is @ property.
Class Student (object): _ sex= "male" _ _ age=0 def get_age (self): return self.__age def set_age (self,age): self.__age = age @ property def age (self): return self.__age
Execute:
> from payhlib import Student > s = Student () > s.age0 > s.set_age (19) > s.age19 > >
In the above we converted the _ _ age private property to the age property, and you might think, since the private attribute is converted to a property, can we modify it directly? The answer is no, because property converts _ _ age to attributes, but it doesn't have the setter function, so we need to add it.
> from payhlib import Student > s = Student () > s.age 0 > s.age=20Traceback (most recent call last): File ", line 1, in AttributeError: can" t set attribute >
Add setter method
Class Student (object): _ sex= "male" _ _ age=0 def get_age (self): return self.__age def set_age (self,age): self.__age= age @ property def age (self): return self.__age @ age.setter def age (self,value): self.__age=value
Execute:
> from payhlib import Student > s = Student () > > s.age0 > s.age=20 > s.age 20 > this is the end of the article on "how to use @ property of Python". Thank you for reading! I believe everyone has a certain understanding of the knowledge of "how to use Python @ property". If you want to learn more, 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.