In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article focuses on "how to use @ property correctly". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor learn how to use @ property correctly.
Here's what they say:
Class People: def _ _ init__ (self, name): self.name = name self._work = 'haven't found a job yet' @ property def work (self): return self._work @ work.setter def work (self, value): self._work = value
The running effect is shown in the following figure:
But in fact, there is no need for the @ property decorator to exist in this code, and the code can be further simplified:
Class People: def _ _ init__ (self, name): self.name = name self.work ='I haven't found a job yet'
The result is exactly the same:
So what's the point of using the @ property decorator?
Indeed, in the above example, there is no need for the @ property decorator to exist, because here reading the properties of an object is just "returning data". But in some cases, it is not only to read, but also to calculate.
Let me give you an example. I don't know if you have such an experience. You just looked at your phone and found that the time is 23:10. 30 seconds later, your friend happens to ask you what time it is, and you immediately answer: 23:10. As soon as he looked at his watch, it was. So exclaimed, how come you know the time without looking at your watch?
For example, we are now going to implement a ProxyProvider class that reads Redis, gets the latest proxy IP, and returns a random one. There is another program that adds a new proxy IP to Redis. But the frequency is not high.
So, the class ProxyProvider, you don't need to read the database every time you get the IP, you can read it every hour. If you don't use the @ property decorator, you might write the code like this:
Import time import random class ProxyProvider: def _ init__ (self): self.pool = [] self.last_update_time = 0 def get_proxy (self): now = time.time () if now-self.last_update_time > 3600 or not self.pool: selfself.pool = self.get_all_proxies_from_redis () return random.choice (self.pool)
If you often look at Java code, you will find a lot of this way of writing get_xxx and set_xxx.
So, when you call, call like this:
Provider = ProxyProvider () provider.get_proxy ()
If you use @ property, the code can be rewritten as follows:
Import time import random class ProxyProvider: def _ init__ (self): self.pool = [] self.last_update_time = 0 @ property def proxy (self): now = time.time () if now-self.last_update_time > 3600 or not self.pool: selfself.pool = self.get_all_proxies_from_redis () return random.choice (self.pool)
So when you read it, write:
Provider = ProxyProvider () provider.proxy # Note that there are no parentheses here
As we can see, the overall code logic is the same, and there is no simplification in the code. However, when calling, the former invokes a method and the latter reads a property.
Similarly, if you want to modify the data, you need to implement a set_xxx method when you don't use @ property. But using @ property to decorate a method, you can also implement some internal logic when setting up the data, such as:
Import time import random class ProxyProvider: def _ init__ (self): self.pool = [] self.special_ip = set () self.last_update_time = 0 @ property def proxy (self): now = time.time () if now-self.last_update_time > 3600 or not self.pool: selfself.pool = self.get_all_proxies_ From_redis () return random.choice (self.pool + list (self.special)) @ proxy.setter def proxy (self Value): if not value.startswith ('http'): proxy = f' http://{ip}' if proxy in self.special_ip: return self.special_ip.add (proxy)
For the caller, the complex checking logic is transparent:
Provider = ProxyProvider () provider.proxy = '123.45.67.89'
For people accustomed to Java, they may like to explicitly write get_xxx and set_xxx methods. But for people who are used to Python, I think using @ property will make the code more readable.
At this point, I believe you have a deeper understanding of "how to use @ property correctly". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.