In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the relevant knowledge of "how to use Python _ iter__". In the operation of actual cases, many people will encounter such a dilemma. Next, 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!
Let's first look at an example:
Class Fib: def _ _ init__ (self Max): self.max = max def _ iter__ (self): print ('_ iter__ called') self.a = 0 self.b = 1 return self def _ next__ (self): print ('_ next__ called') fib = self.an if fib > self.max: raise StopIteration self.a, self.b = self.b Self.a + self.b return fibfor i in Fib (3): print (I) # output _ _ iter__ called__next__ called0__next__ called1__next__ called1__next__ called2__next__ called
Understand _ _ iter__ through this Fibonacci sequence generator.
Defining _ _ iter__ means that the class is an iterator. It runs only once at the beginning of the iteration. The object itself is returned. Here, two attributes an and b are added to the object easily. The next step is to call _ _ next__ in a loop until raise StopIteration is encountered. The process of calling is the process of simulating the Fibonacci sequence.
11 2 3 5 7 11 18... As you can see, the value of self.an is the value of the sequence. We just need to output this value through the variable fib for each iteration. When self.a = 3, a false assignment to fib,fib > self.max exits the iteration. The trick is to let the sequence iterate over itself and output it with an intermediate variable, fib.
In an iterator, _ _ iter__ and _ _ next__ are required, but _ _ init__ is not.
Class Fib: def _ _ iter__ (self): print ('_ iter__ called') self.a = 0 self.b = 1 self.max = 3 return self def _ next__ (self): print ('_ next__ called') fib = self.an if fib > self.max: raise StopIteration self.a, self.b = self.b Self.a + self.b return fib
The output of the above code is consistent with the first paragraph of code. Since _ _ iter__ is only allowed once, it can be used to assign values to attributes. However, such a Fib class cannot be constructed by passing in parameters. Self.max is built in.
In order to deepen our understanding, let's give another example. Given the first term A1, step d, returns an arithmetic sequence of the last term closest to n.
# isometric sequence formula an = A1 + (n Mel 1) * dclass Acu (): def _ init__ (self, A1, d) N): self.a1 = A1 self.d = d self.n = n def _ _ iter__ (self): return self def _ next__ (self): an = self.a1 if an > self.n: raise StopIteration else: self.a1 + = self.d return anfor i in Acu (1,2,15): print (I)
For exactly the same reason, first use iter to indicate that the object is an iterator, then call next, first assign the first term A1 to an and output it. A1 increases the step size before output. In this way, the value of an remains the same, but it will change the next time it is assigned through A1.
This is the end of the content of "how to use Python's _ _ iter__". 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.
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.