While reading the source of the Wiki module in Trac, I discovered that the key word of yiled was used in many places.
The feeling is to replace return with yield in the place where you need to return a value,
Not very clear about its usage, so study it carefully.
A function that uses the Yiled keyword is no longer a normal function, but a generator function (generator functions).
An iterator (iterator) is returned when the function is called.
So the two concepts of iterators and generators are explained below.
I. iterators (Iterator)
Writes that an iterator is an object that implements an iterator protocol,
There are generally two ways to implement
1) Next method
Returns the next element of the container
2) __iter__ method
Returns the iterator itself
For the for language you may not be unfamiliar, we often need to traverse some container objects will be used to the FOR loop:
Python code
- l=[0,1,2,3,4,5,6]
- For I in L:
- Print I
L is an object of type list, this section of the For-in code is actually called the __iter__ () function of L when it is running, returns an implementation of __next__ () or next (each version of Python may not be the same, I experimented with an iterator object that used the version 2.6.2), and each time the loop was taken to remove an element from next.
Of course, there is no need to put all the elements into a list or other container class to cycle, which is a waste of space.
We can create one of our own iterators directly.
Python code
- #-*-Coding:utf-8-*-
- "' 'Fibonacci iterator '
- Class Fib:
- " an iterator that can generate a Fibonacci sequence"
- def __init__ (self, max):
- Self.max = Max
- def __iter__ (self):
- SELF.A = 0
- self.b = 1
- return self
- def next (self):
- FIB = SELF.A
- if fib > self.max:
- Raise Stopiteration
- SELF.A, self.b = self.b, self.a + self.b
- return fib
With this Fibonacci iterator defined, we're ready to use it.
Python code
- From Fibonacci2 import Fib
- For-N in Fib:
- Print n
When the FIB (1000) is called, an iterator object is generated, each time the loop is called next to fetch the next value.
So we can see that the iterator has a very central thing is that in the loop, the iterator can remember the previous state.
Two. Generator
As we said before, any function that uses the yield keyword is no longer a normal function, so let's take a look at the example, which is easier to understand.
Python code
- def fib (max):
- A, B = 0, 1
- While a < max:
- yield a
- A, B = B, A + b
Here a few lines of code implement the above iterator class so a lot of code implemented by the function
It is very similar to the above when used:
Python code
- From Fibonacci import fib
- For-N in fib:
- Print n
The citation fib function uses yield so it is a generator function, and when we call the FIB (1000) It actually returns an iterator, and this iterator can control the operation of the generator function.
We control the operation of the FIB generator function through the action of the returned iterator.
Whenever the next function of the iterator is called, the generator function runs to the yield, returns the value after yield and pauses in this place, and all States are held until the next function is called, or an exception loop exits.
So the concept of generator is still very simple.
Three. Summary
1.for-in statements operate on an iterator object at the bottom.
2. A function that uses the yield keyword is a generator function that, when called, generates an iterator that can control its own run.
From the yield of Python,