One of sex. The main reason is that there is no concept of generator in other mainstream languages. It is precisely because the generator is a
A "new" thing, on the one hand, did not attract the attention of engineers, on the other hand, it also increased the learning cost of engineers.
Eventually, everyone missed such a useful feature in Python.
In this article, I hope to introduce Python's generator in an easy-to-understand way, thus changing "such a useful feature is that"
It is not widely used. The organizational structure of this paper is as follows: Chapter 1 briefly introduces the iterator protocol in Python; In this article
The second chapter will introduce the concept and grammar of generator in detail; In the third chapter, a useful example will be given to illustrate the advantages of using the generator.
Department; Finally, the matters needing attention in using the generator are briefly discussed.
1. iterator protocol
Because the generator automatically implements the iterator protocol, which is also an abstract concept for many people. So, for the sake of
In order to better understand the generator, we need to briefly review the concept of iterator protocol.
The 1. iterator protocol means that the object needs to provide the next method, which either returns the next item in the iteration or causes.
StopIteration exception that terminates iteration.
2. Iteration objects are objects that implement iterator protocols.
3. The Protocol is a convention. Iterative objects can implement iterator protocols and Python built-in tools (such as for loop, sum, min, max functions).
Number and so on. ) using the iterator protocol to access objects.
For example, in all languages, we can use the for loop to traverse arrays. The underlying implementation of Python list is an array, which is
Therefore, we can use the for loop to traverse the list. As follows:
& gt& gt& gt for n:
... printing
However, friends who are slightly familiar with Python should know that Python's for loop can be used not only to traverse linked lists, but also to traverse linked lists.
File object, as follows:
& gt& gt& gt open ('/etc/passwd') provides an iterator protocol for f: # file objects.
... for the line in f: # for cyclically accessing files using iterator protocol.
... print lines
...
Why can files also be traversed by a for loop in Python? This is because in Python, file objects are iterative.
The for loop doesn't know that it is traversing a file object, but it just uses the iterator protocol to access the object. this is because
Python's file objects implement the iterator protocol, so we can access files in such a convenient way, as follows:
& gt& gt& gtf = On ('/etc/passwd')
& gt& gt& gt Director (female)
['__class__ ',' __enter__ ',' __exit__ ',' __iter__ ',' __new__ ',' writelines ','...'
2. Generator
Python uses generators to support deferred operations. The so-called delayed operation is to produce results when needed, not immediately.
Bear fruit. This is also the main benefit of the generator.
Python provides generators in two different ways:
20 17/ 1 1/6 How to better understand Python iterators and generators? -Zhihu
/Question /20829330 2/5
1. generator function: general function definition, but return statement is yield instead of return statement to return the result. The yield statement returns one at a time.
As a result, in the middle of each result, the state of the function is suspended so as to continue execution from where it left off next time.
2. Generator expression: Similar to list derivation, but the generator returns an object that produces results on demand instead of building one at a time.
Result list
2. 1 generator function
Let's look at an example that uses a generator to return the square of a natural number (notice that multiple values are returned):
Define the root (n):
For I(N) in the range:
Output i ** 2
For the item in gensquares(5):
Print items,
Use normal functions:
Define the root (n):
res = []
For I(N) in the range:
Additional resources (i*i)
Return resources
For the item in gensquares(5):
Print items,
As you can see, there is very little code that uses generator functions.
2.2 generator expression
With list derivation, all results will be generated at once:
& gt> squares = [x * * 2] of x in range (5)]
& gt& gt& gt plaza
[0, 1, 4, 9, 16]
Replacing parentheses in a list with parentheses is a generator expression:
& gt& gt& gtsquares = (x**2 of x in range (5))
& gt& gt& gt Square & gt> Next (Square)
& gt& gt& gt Next (box)
1
& gt& gt& gt Next (box)
four
& gt& gt& gt list (square)
[9, 16]
Python not only uses the iterator protocol, but also makes the for loop more universal. Most built-in functions also use the iterator protocol to access objects.
Yes For example, the sum function is a built-in function of Python, which uses iterator protocol to access objects, and the generator realizes iterator coordination.
20 17/ 1 1/6 How to better understand Python iterators and generators? -Zhihu
/question/20829330 3/5
Discussion, so we can directly calculate the sum of a series of values like this:
& gt>> sum (x ** 2 of x in xrange (4))
Instead of creating a list first:
& gt>> sum (x * * 2)] of x in [xrange (4)].
2.3 Check the generator again.
We have a perceptual knowledge of generators. Take the generator function as an example, and then discuss the Python generator in depth:
1. Syntactically similar to a function: the generator function is almost the same as a regular function. They are all defined by def statement, the difference is that
In, the generator uses the yield statement to return the value, while the regular function uses the return statement to return the value.
2. Automatically implement the iterator protocol: For the generator, Python will automatically implement the iterator protocol so that it can be applied to the iteration background (for example
Loop, summation function). Since the generator automatically implements the iterator protocol, we can call its next method.
When there is no value to return, the generator automatically generates a StopIteration exception.
3. State Suspend: The generator uses the yield statement to return the value. The yield statement suspends the state of the generator function and retains enough information.
So as to continue execution where it left off.
Step 3: Example
Let's look at two examples of generators, so that we can better understand the role of generators.
First of all, the advantage of the generator is that it delays calculation and returns one result at a time. In other words, it won't generate all the results at once, which is very important for large.
Data processing can be very useful.
You can try the following two expressions on your computer to observe the memory usage. For the previous statement, I am in my own electricity.
Tested on the brain, the computer has been stuck before seeing the final result, and there is almost no memory occupation for the later expression.
Sum ([I to I in x range (1000000000)])
Sum (I for I in x range (1000000000))
Besides delaying calculation, the generator can also effectively improve the readability of the code. For example, there is a requirement that every word appears in a paragraph.
The location of.
Without generator:
Def index_words (text):
Result = []
If the text:
Result. Added (0)
For indexes, the letters in the enumeration (text, 1):
If the letter = = "":
Results. Append (index)
return result
Using a generator:
20 17/ 1 1/6 How to better understand Python iterators and generators? -Zhihu
/question/20829330 4/5
Def index_words (text):
If the text:
Output 0
For indexes, the letters in the enumeration (text, 1):
If the letter = = "":
Yield index
Here, there are at least two good reasons why using the generator is clearer than not using the generator code:
1. After using the generator, there are fewer lines of code. Remember, if you want to write code in a Pythonic way, before ensuring the readability of the code.
By the way, the fewer lines of code, the better
2. When the generator is not used, for each result, we first see the result.append(index), and then the index.
In other words, every time we see an append operation to the list, only the append operation is the result we want. Use build
, directly yield index, without the interference of list append operation, you can see at a glance that the code is to be returned.
Index.
This example fully shows that the reasonable use of the generator can effectively improve the readability of the code. As long as everyone fully accepts the concept of generator,
Understand that the yield statement, like the return statement, also returns a value. Then, you can understand why it is better to use a generator than not to use it.
It is better to understand that using the generator can really make the code clear and easy to understand.
4. Matters needing attention in using the generator
I believe that through this article, you can understand the role and benefits of generators. However, this is not over yet. Using a generator is also a little bit.
Precautions.
Let's look directly at the example. Assume that the total population of each province is kept in the document. Now it is necessary to require the population of each province to account for the total population of the country.
The proportion of. Obviously, we need to find out the total population of the country first, and then traverse the population of each province and divide it by the total population.
Proportion of provincial population to national population.
As follows:
Def get_province_population (file name):
Use open (file name) as f:
For lines in f:
Earnings Interest (Line)
gen = get _ province _ population(' data . txt ')
All _ population = sum (gender)
# Print All _ Population
For population by sex:
Print Population/All _ Population
Execute the above code, there will be no output, because the generator can only traverse once. When we execute the sum statement,
We have traversed our generator, and when we traverse our generator again, there will be no record. So, the code above doesn't
There will be any output.
Therefore, the only thing to note about the generator is that the generator can only traverse once.
summary
20 17/ 1 1/6 How to better understand Python iterators and generators? -Zhihu
/Question /20829330 5/5
This paper introduces an important feature of Python that is easily overlooked, that is, Python generator. To explain this generation
This paper first introduces iterator protocol, then introduces generator function and generator expression, and demonstrates the advantages of generator through examples.
And matters needing attention. In practical work, making full use of Python generator can not only reduce memory occupation, but also improve code readability.
Mastering the generator is also a standard for Python masters. I hope this article will help you understand Python's generator.