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

How to use property of python

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article introduces the knowledge of "how to use the property of python". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

I. linkage

Case study:

Class Goods:def _ init__ (self, name, price, count=1): self.name = name # Commodity name self.price = price # Commodity Unit Price self.count = float (count) # Commodity quantity self.total = price*count apple = Goods ('Apple', 5,2) print (apple.total) apple.count = 3print (apple.total) out:1010

In the above case, a Goods (commodity) class is established, which has name (commodity name), price (commodity unit price), and count (commodity quantity). It also has a toatl (Total Price of goods) attribute, which is calculated based on the unit price multiplied by the quantity.

Then we build an example: apple, the name is Apple, the unit price is 5, the quantity is 2. Now the print apple.total display is 10, the result is normal, but when we change the quantity to 3 and then print the apple.total result is still 10, the data does not produce linkage! Be sure to avoid this when programming.

Positive solution:

Class Goods:def _ init__ (self, name, price, count=1): self.name = name # Trade name self.price = price # Commodity Unit Price self.count = float (count) # Commodity quantity @ propertydef total (self): # Total Commodity Price return self.price*self.countapple = Goods ('Apple', 5,2) print (apple.total) apple.count = 3print (apple.total)

In this code, we can get the linked total property by disguising the total method as a property.

Well, remember the first advantage of using property-data linkage.

Second, robust

Continue the above case of robust, assuming that the quantity of goods is manually entered by the salesperson, what will happen if the salesperson accidentally enters the quantity into'a'?

Class Goods:def _ init__ (self, name, price, count=1): self.name = name # Commodity name self.price = price # Commodity Unit Price self.count = float (count) # quantity @ propertydef total (self): # Total Commodity Price return self.price*self.countapple = Goods ('Apple', 5,'a') print (apple.total) out:ValueError: could not convert string to float:'a'

This program directly reported an error, abnormal exit! Of course, you can blame the salesperson for his carelessness, but at the same time, you can't deny that the fault tolerance rate of your code is low, that is, if you are not robust, you often report mistakes! Qualified programmers must take into account a variety of situations when writing code to improve the robustness of the program. Let's correct the code for the above situation:

Class Goods:def _ init__ (self, name, price, count=1): self.name = name # Commodity name self.price = price # Commodity Unit Price self.__count = count # Commodity quantity @ propertydef count (self): try:self.__count = float (self.__count) except ValueError:print ('input quantity is incorrect, the value is automatically corrected to 1.0, please check!') Self.__count = 1.0return self.__count @ count.setterdef count (self, n): try:n = float (n) except ValueError:print ('input quantity is incorrect, the value is automatically corrected to 1.0, please check!') N = 1.0self.__count = n @ propertydef total (self): # Total commodity price return self.price*self.countapple = Goods ('Apple', 5,'a') print (apple.total) apple.count = 'bb'print (apple.total) out: the input quantity is incorrect and the value is automatically corrected to 1.0. please check! 5.0input quantity is incorrect, the value is automatically corrected to 1.0. please check!

Notice the @ function name .setter, which should be the same as the function name on the next line of the previous @ property.

III. Safety

Continue to talk about security in the above code, an important part of data security is to ensure that the data value is in a reasonable range, if you do not check the value range of important data, a major error will occur. What happens if a salesperson changes the sales quantity to a negative number in the above case? It is obvious that there will be accounting errors, so we have to check the count value and cannot accept negative numbers.

Therefore, the @ count.setter section must be modified:

@ count.setterdef count (self, n): try:n = float (n) if n < 0:print ('input quantity is incorrect, the value is automatically corrected to 0, please check!') N = 0except ValueError:print ('input quantity is incorrect, the value is automatically corrected to 1. 0, please check!') N = 1.0self.__count = n

Finally, add the deleter section of property, which removes the _ _ count attribute when calling del apple.count:

@ count.deleterdef count (self): this is the end of del self.__count 's "how to use property for python". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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