In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "what is the method of yield return generator in Python". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what is the method of yield return generator in Python".
I. iterator
Read the list item by item, called iteration.
Mylist = [1,2,3] for i in mylist: # iterable object print (I)
A list parser is also an iterator.
Mylist = [Xerox for x in range (3)] for i in mylist: print (I)''014 stories'
All for...in... Are iterators, including lists, strings, files, and so on.
However, all the values of the iterator are stored in memory, which is a waste of memory.
So there is the concept of generator.
2. Generator
A generator is an iterator that can only be iterated once.
The generator does not store all values at once, but generates values dynamically.
Mygenerator = (Xerox for x in range (3)) for i in mygenerator: print (I)
The generator can only be executed once, and nothing is output when it is executed again.
3. Yield1. Example one
Yield is similar to the return keyword, except that the function returns a generator.
# create generator def createGenerator (): mylist = range (10) for i in mylist: print (I) # verify that yield i*imygenerator = createGenerator () print (mygenerator) is not executed when the function call is called # # use generator for i in mygenerator: print (I) # return null value again
The function returns a set of values that only need to be read once, which can greatly improve the performance of the code.
When a function is called, the code in the function body does not execute, and the function only returns the generator object.
The code continues each time from where it stopped when using the generator.
two。 Example 2 # Python Learning Exchange Group: 53150902 learn another example def foo (): print ("starting...") The while True: res = yield 4 # function does not actually execute print ("res:" Res) g = foo () # get a generator object print (next (g)) # actually execute print ("*" * 20) print (next (g)) # continue to execute''starting...4*res: None4'''print (g.send (7)) from where it was last stopped
The yield does not jump out of the while loop until it is executed.
The next function is used to perform the next step.
The send function is used to send a parameter to the generator. And the send method contains the next method.
Thank you for reading, the above is the content of "what is the method of yield return generator in Python". After the study of this article, I believe you have a deeper understanding of what the method of yield return generator in Python is, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.