Python yield usage

Source: Internet
Author: User

As a relatively new application language, the Python programming language has many methods that developers need to master. For example, the Python yield introduced today is a special application. Yield's English words mean production. I was very confused when I first came into contact with Python and never understood how to use Python yield. Just roughly knowing that yield can be used to insert data into a function's return value, for example, the following example:

 
 
  1. def addlist(alist):  
  2. for i in alist:  
  3. yield i + 1 

Take out each item of alist and insert I + 1. Then retrieve each item by calling:

 
 
  1. alist = [1, 2, 3, 4]  
  2. for x in addlist(alist):  
  3. print x, 

This is indeed an example of the Python yield application. However, after reading limodou's article "2.5 yield's learning experiences" and having repeated experiences, I have a new understanding of yield.

1. functions containing yield

If you see that a function contains yield, this means that this function is already a Generator and its execution will be much different from other common functions. For example, the following simple function:

 
 
  1. def h():  
  2. print 'To be brave'  
  3. yield 5  
  4. h() 

We can see that after calling h (), the print statement is not executed! This is yield. How can I execute the print statement? This is the question to be discussed later. Through further discussions and learning, we will understand the working principle of yield.

2. yield is an expression.

Before Python2.5, Python yield was a statement, but in 2.5, yield is an Expression, for example:

 
 
  1. m = yield 5 

The Return Value of the expression (yield 5) is assigned to m. Therefore, m = 5 is incorrect. So how can we get the return value of (yield 5? You need to use the send (msg) method described later.

3. view the principle through the next () Statement

Now let's reveal how yield works. We know that the above h () is not executed after it is called because it has a yield expression. Therefore, we use the next () statement to execute it. The next () Statement resumes the execution of Generator until the next yield expression. For example:

 
 
  1. def h():  
  2. print 'Wen Chuan'  
  3. yield 5  
  4. print 'Fighting!'  
  5. c = h()  
  6. c.next()c.next() 

After the call, h () starts to execute until yield 5 is encountered, so the output result is:

 
 
  1. Wen Chuan 

When we call c. next () Again, it continues until the next yield expression is found. Because there is no Python yield later, an exception is thrown:

 
 
  1. Wen Chuan  
  2. Fighting!  
  3. Traceback (most recent call last):  
  4. File "/home/evergreen/Codes/yidld.py", line 11, in <module> 
  5. c.next()  
  6. StopIteration 

4. send (msg) and next ()

After learning how to execute a function that contains yield, let's look at another very important function send (msg ). In fact, next () and send () are similar in a sense. The difference is that send () can pass the value of the yield expression, while next () cannot pass a specific value, only None can be passed in. Therefore, we can see that c. next () and c. send (None) have the same effect. Let's look at this example:

 
 
  1. Def h ():
  2. Print 'wen chuanc ',
  3. M = yield 5 # Fighting!
  4. Print m
  5. D = yield 12
  6. Print 'we are together! '
  7. C = h ()
  8. C. next () # equivalent to c. send (None)
  9. C. send ('fighting! ') # (Yield 5) expression is granted with 'fighting! 'Output result:
  10. Wen Chuan Fighting!

Please note that when calling for the first time, use the next () statement or send (None) statement. You cannot use send to send a non-None value. Otherwise, errors may occur, because there is no Python yield statement to receive this value.

5. return values of send (msg) and next ()

Send (msg) and next () return values. Their return values are special. They return parameters of the next yield expression. For example, yield 5 returns 5. So far, do you understand something? In the first example of this article, we use for I in alist to traverse the Generator. in fact, alist is called every time. next (), and each time alist. the return value of Next () is exactly the yield parameter, that is, the stuff we thought was pressed in. Let's continue the above example:

 
 
  1. Def h ():
  2. Print 'wen chuanc ',
  3. M = yield 5 # Fighting!
  4. Print m
  5. D = yield 12
  6. Print 'we are together! '
  7. C = h ()
  8. M = c. next () # m obtains the yield 5 parameter value 5
  9. D = c. send ('fighting! ') # D obtains the yield 12 parameter value 12
  10. Print 'we will never forget the date', m, '.', d output result:
  11. Wen Chuan Fighting!
  12. We will never forget the date 5. 12

6. throw () and close () interrupt Generator

Interrupt Generator is a very flexible technique. You can terminate Generator by throwing a GeneratorExit exception in throw. The Close () method works the same. In fact, it calls throw (GeneratorExit) internally. We can see:

 
 
  1. def close(self):  
  2. try:  
  3. self.throw(GeneratorExit)  
  4. except (GeneratorExit, StopIteration):  
  5. pass  
  6. else:  
  7. raise RuntimeError("generator ignored GeneratorExit")  
  8. # Other exceptions are not caught 

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

 
 
  1. Traceback (most recent call last ):
  2. File "/home/evergreen/Codes/yidld. py", line 14, in <module>
  3. D = c. send ('fighting! ') # D obtains the yield 12 parameter value 12
  4. StopIteration

The above is our introduction to Python 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.