Python in-depth understanding yield

Source: Internet
Author: User

Http://www.jb51.net/article/15717.htm

It's just a rough idea. Yield can be used to return value plug data for a function, such as the following example:

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:

For x in Addlist (alist):
Print X,

This is indeed an example of yield application, but, having read Limodou's article, "2.5 yield learning experience ", and repeated experience, the yield has a new understanding.

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:

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:

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:

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:

"/home/evergreen/codes/yidld.py", line one, in <module>
C.next ()
StopIteration4. Send (MSG) with 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:

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) expression is given ' 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:

print ' Wen Chuan ',
m = Yield 5 # fighting!
Print m
D = Yield 12
print ' We are together! '

c = h ()
m = C.next () #m Gets the parameter value of yield 5 5
D = c.send (' fighting! ') #d obtained the parameter value of yield 12 12
print ' We'll never forget the date ', M, '. ', D

Output Result:
Wen Chuan fighting!
We'll 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:

Try
Self.throw (Generatorexit)
Except (Generatorexit, stopiteration):
Pass
Else
Raise RuntimeError ("generator ignored Generatorexit")
# Other exceptions is not caught

Therefore, when we call the close () method and then call next () or send (msg), an exception is thrown:

"/home/evergreen/codes/yidld.py", line +, in <module>
D = c.send (' fighting! ') #d obtained the parameter value of yield 12 12
Stopiteration

Python in-depth understanding yield

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.