In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "how to use getter and setter in python". In daily operation, I believe many people have doubts about how to use getter and setter in python. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to use getter and setter in python". Next, please follow the editor to study!
Replicative compilation
In python, you can write getter and setter methods directly in combination with the writing habits in java. Here is an example:
Class DataBean: "" bean class "def _ _ init__ (self):" Construction method "self.username: str ="self.password: str ="def set_username (self)" Username: str): "set user name" self.username = username def set_password (self Password: str): "set password"self.password = password def get_username (self):" get user name "return self.username def get_password (self):" get password "return self.password"
This is the getter and setter structure written by combining the habits of java and the use of the python class. Of course, it can also be written exactly as in java. Refer to the following example:
Class DataBean: "" bean class "" # username username: str = "" # password password: str = "" def set_username (self, username: str): "set user name" self.username = username def set_password (self Password: str): "set password"self.password = password def get_username (self):" get user name "return self.username def get_password (self):" get password "return self.password"
In fact, it is to extract the variables defined by self in the constructor, and the effect is consistent.
Decorator using python
Among python's built-in decorators, @ property and @ XXX.setter are the best choices for getter and setter methods.
When the ultimate goal of a method (function) is to return a value, you can decorate the method with @ property (I prefer to call it an annotation because of the usage of java), so that the getter method can be achieved.
When decorated with XXX.setter on top of a method, the representation can be directly through the class instance object name. The variable name is its variable assignment, where XXX represents the variable name and also the method (function) name. For more information, please see the following example:
Class DataBean: "" bean class "" # replace the setter method @ username.setter def username (self, username: str): "set the user name" self.username = username @ password.setter def password (self) " Password: str): "" set password "" self.password = password # instead of getter method @ property def username (self): "get user name" return self.username @ property def password (self): " Get the password "" return self.password# test data_bean = DataBean () # directly assign date_bean.username = "123" print (data_bean.username) to this The study on "how to use getter and setter in python" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.