In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces the python iterable object, iterator, generator, collaborative program instance analysis related knowledge, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe you will have something to gain after reading this python iterable object, iterator, generator, collaborative program instance analysis article, let's take a look.
Design patterns: iteration
Iteration is a design pattern that solves the problem of ordered convenient sequences. Generic iterable objects need to support done and next methods.
The pseudo code is as follows:
While not iterator.done (): item = iterator.next () .python: iterable objects and iterators
The iterable object of python needs to implement the _ _ iter__ () method, which returns an iterator. The for loop and the top-level function iter (obj) call the _ _ iter__ () method of obj and return an iterator. The iterator itself is also an iterable object, so you also need to implement the _ _ iter__ () method to return itself, and you also need to implement the _ _ next__ () method to get the next element.
Example of a simple class:
Class Iterable: def _ _ init__ (self,string): self.string = string def _ _ iter__ (): return Iterator (self.string) class Itrator: def _ init__ (self String): self.string = string self.words = list (string) self.index = 0 def _ _ iter__ (self): return self def _ next__ (self): if self.index = = len (self.words): raise StopIteration # element traversal completion throws error The for loop automatically processes item = self.words [self.index] self.index + = 1 return item. Why should there be a generator?
The generator helps iterators save memory. As you can see in the above example, an iterable object generates and saves all elements at once. But sometimes we only focus on the elements we are currently working on. If the number of elements is large, for example, when processing a large number of log analysis, all rows are loaded into memory at once, resulting in a serious memory waste. That's why there's a generator.
Generator implementation of python
The yield keyword makes the python generator super convenient. Yield can be seen as a code execution pause until the next call to the next () method, and then paused again when you encounter the next yield. In addition, yield xxx indicates back to the element xxx.
Def my_generator (stirng): for x in string: yield x
It is important to note that although we define a function, in fact, python automatically converts it into a generator object rather than a normal function object.
Cooperative process
The co-program is used to allow us to send data to the generator. The syntax difference between the co-program and the generator is: xx = yield xxx, that is, there is an assignment statement on the left side of the yield, the send (a) method assigns a to the xx, and the co-program object returns xxx. This feature of the co-program is used for asynchronous and concurrent programming to automatically pause switching when the program encounters IO.
The order of execution of the collaboration program:
Yield appears and generator pauses
The send () method is executed outside the function, and the generator is activated
The value that occurs is assigned to the variable on the left side of the yiled statement
The generator continues to execute until the next yield statement is encountered.
This is the end of the article on "python iteratable objects, iterators, generators, and instance analysis of collaborative programs". Thank you for reading! I believe you all have a certain understanding of the knowledge of "python iterable objects, iterators, generators, and collaborative program instance analysis". 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.