Before introducing yield it is necessary to describe the iterator (iterator) and generator (constructor) in the next Python.
One, iterator (iterator)
In Python, for loops can be used for any type in Python, including lists, Ganso, and so on, in fact, for loops are available for any "iteration object," which is actually an iterator
An iterator is an object that implements an iterator protocol, and the iterator protocol in Python is that an object with the next method advances to the next result, and Stopiteration is raised at the end of a series of results. Any such object in Python can be iterated with a for loop or other traversal tool, and the iteration tool will call the next method within each iteration and catch the stopiteration exception to determine when to leave.
An obvious benefit of using iterators is that each time you read only one piece of data from an object, it does not incur excessive memory overhead.
For example, to read the contents of a file row by line, using the ReadLines () method, we can write this:
Copy Code code as follows:
For line in open ("Test.txt"). ReadLines ():
Print Line
This can work, but not the best way. Because he actually loads the file into memory one at a time, and then prints it line by row. This method has a large memory overhead when the file is large.
Using the iterator of file, we can write this:
Copy Code code as follows:
For line in open ("Test.txt"): #use file iterators
Print Line
This is the simplest and fastest-running writing, he did not explicitly read the file, but use the iterator to read down one row at a time.
Second, generator (constructor)
The builder function is associated with the concept of an iterator protocol in Python. In short, functions that contain yield statements are specially compiled genetic builder. When the function is called, they return a generator object that supports the iterator interface. The function may have a return statement, but its function is to yield the resulting value.
Unlike normal functions that generate values and then exit, the generator functions automatically suspend and suspend their execution and status after they generate a value, and his local variables will hold state information, which will be valid again when the function is restored.
Copy Code code as follows:
>>> def g (n):
... for i in range (n):
... yield i **2
...
>>> for I in G (5):
... print I, ":",
...
0:1: 4:9: 16:
To understand how he works, let's look at the next method:
Copy Code code as follows:
>>> t = g (5)
>>> T.next ()
0
>>> T.next ()
1
>>> T.next ()
4
>>> T.next ()
9
>>> T.next ()
16
>>> T.next ()
Traceback (most recent call last):
File "", Line 1, in
Stopiteration
After running 5 Next, the generator throws a Stopiteration exception, and the iteration terminates.
Let's look at an example of a yield, using a generator to generate a Fibonacci sequence:
Copy Code code as follows:
Def FAB (max):
A,b = 0,1
While a < max:
Yield a
A, B = B, a+b
>>> for I in Fab (20):
... print I, ",",
...
0, 1, 1, 2, 3, 5, 8, 13,
See here should be able to understand the generator that very abstract concept of it ~ ~