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 introduces the knowledge of "how to master the Python generator". Many people will encounter such a dilemma in the operation of actual cases, 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!
1. What is an iterable object?
Iterable object, English Iterable, is an adjective. Similar to the Java language, this kind of object can be regarded as a kind of interface, which abstractly describes the ability of things. Therefore, Iterable naturally has the ability to iterate.
As follows, common list,Iterator and so on are Iterable objects:
In [33]: from collections.abc import Iterable,Iterator In [34]: issubclass (list,Iterable) Out [34]: True In [35]: issubclass (Iterator,Iterable)
two。 What is a generator?
The generator is Iterable, and the easiest way to create the generator is through a pair of (), as follows:
In [37]: G = (ionomi for i in [1jin4jin0]) In [38]: G Out [38]:
G is a generator object, generator object
To get the element of the generator object, use the next function, as shown below, to get the first element:
In [39]: next (g) Out [39]: 1
Get the second element:
In [40]: next (g) Out [40]: 16
3. How many ways to create a generator?
As you can see above, you can create a generator object using a pair of ().
Besides, as we all know, the keyword yield. Yield appears in a function, runs to yield, and the returned object is the generator object (generator object).
4. Is the generator an iterator?
The generator object (generator object) must also be an iterator object (Iterator object), such as generator g above, which is validated using the built-in function isinstance and returns True:
In [43]: isinstance (gmeme Iterator) Out [43]: True
Therefore, it has all the features of an iterator, which we discussed in the previous topic, in short, several features of an iterator:
There will be no return.
Iterators do not need to know all the elements of the entire list in advance
There is no need to load all elements into RAM and it is memory-saving (memory-efficient).
The generator also has these features. In addition, it has some unique features, which we can see with yield below.
5. How to describe yield in one sentence?
The use of the yield keyword is also one of the hottest questions on stackoverflow, to borrow an explanation from the hottest answer:
Yield is a keyword that is used like return, except the function will return a generator.
Yield is a special return that returns a generator object.
To tell you the truth, it's only superficial to understand. So, how to master the use of yield?
6. How to master the use of yield?
To be proficient in yield, you must first make one thing clear:
When you call a function with yield, the function does not execute any line, but returns a generator object
To help you understand, create a function with yield:
Def gfun (): mylist = range (3) for i in mylist: yield iTuni g = gfun () print (g)
To deepen your impression, you can debug and verify that the breakpoint hit on the first line of the function has never been hit.
The function with yield is used in conjunction with for, and after the first call to the generator object created in the previous step, it will enter the function body until it encounters a yield return value.
Then, when the for loop enters the function again, it goes straight to the next sentence of yield. Until the generator object becomes empty.
Demonstrate the above process of yield using the following code:
Def createGenerator (): mylist = range (3) for i in mylist: yield iTuni print (iTuni) g = createGenerator () print (g) for gi in g: pass
Refer to the recorded gif:
7. What are the important values of yield?
After the introduction of yield into Python, it has the ability to implement collaborative programs, which is indeed an efficient programming model. The understanding of collaborative programs will be discussed in detail later. Including more advanced functions such as asynchronous, etc., all based on yield.
8. Yield and itertools
The importance and ubiquity of yield also mention that now, a single module of Python is dedicated to managing iterators and generator objects, namely itertools, and the encapsulated method features will be discussed later.
9. Yield use case
Yield implements the floating-point step frange:
Def frange (start,end,step): I = start while i
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.