Yield of the English word meaning is production, just contact with Python is very confused, has not understood the use of yield.
It's just a rough idea. Yield can be used to return value plug data for a function, such as the following example:
123 |
def addlist(alist): for i in alist: yield i + 1 |
Remove each item from the alist and plug the i + 1 in. Each entry is then fetched by calling:
123 |
alist = [ 1 , 2 , 3 , 4 ] for x in addlist(alist): print x, |
This is indeed an example of yield application
1. Functions that contain yield
If you see a function that contains yield, which means that the function is already a generator, its execution will be a lot different from other normal functions. For example, the following simple function:
1234 |
def h(): print ‘To be brave‘ yield 5 h() |
As you can see, after calling H (), the print statement is not executed! This is yield, so how do you get the print statement to execute? This is the question to be discussed later, through the discussion and study later, we will understand how yield works.
2. Yield is an expression
Python2.5 Previously, yield was a statement, but now in 2.5, yield is an expression, such as:
m = Yield 5
The return value of the expression (yield 5) is assigned to M, so that M = 5 is wrong. So how do you get the return value (yield 5)? You need to use the Send (msg) method that is described later.
3. See the principle through the next () statement
Now, let's reveal how yield works. We know that the H () above is called and is not executed because it has a yield expression, so we let it execute through the next () statement. The next () Statement resumes generator execution, and until the next yield expression. Like what:
123456 |
def h ( ): print ' Wen chuan ' yield 5 print ' fighting! ' c = h () c. next () |
After the C.next () call, H () begins execution until yield 5 is encountered, so the output is:
Wen Chuan
When we call C.next () again, we continue execution until we find the next yield expression. Because there is no yield at the back, the exception is thrown:
123456 |
wen chuan fighting! traceback (most recent call last): file "/home/evergreen/codes/yidld.py" , line 11 , in <module> c. next () stopiteration |
4. Send (MSG) and Next ()
Once we see how next () is going to make the function that contains yield, let's look at another very important function, send (msg). In fact, next () and send () function in a certain sense is similar, the difference is that send () can pass the value of the yield expression in, and next () cannot pass a specific value, can only pass the none in. So we can be seen as
C.next () and C.send (None) Act the same.
Take a look at this example:
123456789 |
def
h():
print
‘Wen Chuan‘
,
m
=
yield 5
# Fighting!
print
m
d
=
yield
12
print
‘We are together!‘
c
= h()
c.
next
()
#相当于c.send(None)
c.send(
‘Fighting!‘
)
#(yield 5)表达式被赋予了‘Fighting!‘
|
The result of the output is:
Wen Chuan fighting!
Note that the first call, use the next () statement or send (None), you cannot use Send to send a value other than None, otherwise it will be wrong, because there is no yield statement to receive this value.
5. The return value of Send (msg) and Next ()
Send (MSG) and Next () have return values, their return values are special and return the parameters of the next yield expression. Yield 5, for example, returns 5. Do you understand something here? In the first example of this article, traversing Generator with the for I in Alist actually calls alist each time. Next (), and every time alist. The return value of Next () is exactly the yield parameter, which is what we begin to think is being pressed in. Let's continue with the above example:
12345678910 |
def
h():
print
‘Wen Chuan‘
,
m
=
yield
5
# Fighting!
print m
d
=
yield
12
print
‘We are together!‘
c
=
h()
m
=
c.
next
()
#m 获取了yield 5 的参数值 5
d
=
c.send(
‘Fighting!‘
)
#d 获取了yield 12 的参数值12
print
‘We will never forget the date‘
, m,
‘.‘
, d
|
Output Result:
12 |
Wen Chuan Fighting! We will never forget the date 5 . 12 |
6. Throw () and close () interrupt Generator
Interrupt generator is a very flexible technique that can be used to terminate generator by throwing a generatorexit exception. The Close () method works the same, but inside it is called throw (generatorexit). We look at:
12345678 |
def
close(
self
):
try
:
self
.throw(GeneratorExit)
except
(GeneratorExit, StopIteration):
pass
else
:
raise
RuntimeError(
"generator ignored GeneratorExit"
)
# Other exceptions are not caught
|
Therefore, when we call the close () method and then call next () or send (msg), an exception is thrown:
1234 |
Traceback (most recent call last): File "/home/evergreen/Codes/yidld.py" , line 14, in <module> d = c.send( ‘Fighting!‘ ) #d 获取了yield 12 的参数值12 StopIteration |
http://www.qytang.com/
http://www.qytang.com/cn/list/29/
http://www.qytang.com/cn/list/28/358.htm
http://www.qytang.com/cn/list/41/
http://www.qytang.com/cn/list/37/
http://www.qytang.com/cn/list/46/
http://www.qytang.com/cn/page/19.htm
http://www.qytang.com/cn/list/32/
http://www.qytang.com/cn/list/28/
http://www.qytang.com/cn/list/25/
http://www.qytang.com/cn/list/28/625.htm
http://www.qytang.com/cn/list/28/612.htm
http://www.qytang.com/cn/list/28/611.htm
http://www.qytang.com/cn/list/28/610.htm
In-depth understanding of yield--Yi Tang