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:
- def addlist(alist):
- for i in alist:
- yield i + 1
Take out each item of alist and insert I + 1. Then retrieve each item by calling:
- alist = [1, 2, 3, 4]
- for x in addlist(alist):
- 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:
- def h():
- print 'To be brave'
- yield 5
- 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:
- 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:
- def h():
- print 'Wen Chuan'
- yield 5
- print 'Fighting!'
- c = h()
- c.next()c.next()
After the call, h () starts to execute until yield 5 is encountered, so the output result is:
- 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:
- 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 ()
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:
- Def h ():
- Print 'wen chuanc ',
- M = yield 5 # Fighting!
- Print m
- D = yield 12
- Print 'we are together! '
- C = h ()
- C. next () # equivalent to c. send (None)
- C. send ('fighting! ') # (Yield 5) expression is granted with 'fighting! 'Output result:
- 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:
- Def h ():
- Print 'wen chuanc ',
- M = yield 5 # Fighting!
- Print m
- D = yield 12
- Print 'we are together! '
- C = h ()
- M = c. next () # m obtains the yield 5 parameter value 5
- D = c. send ('fighting! ') # D obtains the yield 12 parameter value 12
- Print 'we will never forget the date', m, '.', d output result:
- Wen Chuan Fighting!
- 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:
- 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 will be thrown:
- Traceback (most recent call last ):
- File "/home/evergreen/Codes/yidld. py", line 14, in <module>
- D = c. send ('fighting! ') # D obtains the yield 12 parameter value 12
- StopIteration
The above is our introduction to Python yield.