python in yield usage (simple and clear)

First of all I want to Tucao about, the process of watching program met yield keyword, then Baidu, found no one can simply let me know, it really speaks TM are clearly and logically, what parameters, what passed, and yet they say their tutorial is the simplest, most easy to understand, and I would like to ask has not considered the feelings of the reader.

Next is the question:

First of all, if you do not have a preliminary understanding of yield points, then you should first yield regarded as "return", this is intuitive, it is first and foremost a return, normal return is what is meant is to return a value in the program and then return to the program is no longer running down the. After seen as a return then it is seen as part of a generator (generator) (with yield function is the real iterator), well, if you do not understand these, then seen as the first yield return, then look directly following program, you will understand the full meaning of the yield:

foo DEF ():
Print ( "Starting ...")
the while True:
RES = the yield. 4
Print ( "RES:", RES)
G = foo ()
Print (Next (G))
Print ( "*" * 20 is)
print (next (g))
as simple as a few lines of code to let you know what is the yield, the output of this code:

... Starting
4
********************
RES: None
4
me directly interpreted code run sequence, the equivalent of stepping through code:

1. After the program started, because the function foo have the yield keyword, so the function foo and not really perform, but to get a generator g (equivalent to an object)

2. until you call the next method, function foo officially started, perform print method foo function, and then enter the while loop

3. After the program encounters the yield keyword, then the yield to think about the return, return a 4, the program stops and does not perform the operation assigned to the res, at this time next (g) statement is executed, the output of the first two lines (the first is a result of the above print while the second is to return a result) is the result of performing print (next (g)), and

4. The program execution print ( "*" * 20), the output 20 *

5. began to perform the following print (next (g)), and on top of that this time almost, but the difference is, this time from the next place to just stop the program started, that is to execute the assignment res , this time to pay attention to the right this time the assignment is no value (as just that is the return out, left and did not give the assignment to pass parameters), so this time res assignment is None, so then the following output is res: None,

6. The program will continue in while there, met again yield, this time to return the same 4, then the program stops, the return is 4 out of 4 print output function.

 

Here you might understand the relationship between yield and return and distinguished, with a yield is a function generator, rather than a function, and this has generated is a function next function, next to the equivalent of "next" generation which number, place this time next start is followed by the next stop on a place of execution, so call the next time, the generator will not start executing function foo from the previous step and then just stop place to start, then the event to the yield, return the number to be generated, this step is over.

****************************************************************************************************************************************

foo DEF ():
Print ( "Starting ...")
the while True:
RES = the yield. 4
Print ( "RES:", RES)
G = foo ()
Print (Next (G))
Print ( "*" * 20 is)
print (g.send (7))
look a send function generator of this example that the last line put replaced the above example, the output:

... Starting
4
********************
RES: 7
4
first generally talk about the concept of send function: At this point you should note that the word purple above well above that value of res Why is None, this becomes a 7, in the end why, it is because, send a parameter is sent to the res, as mentioned above, return time and did not assign res 4 , next time when the execution had to continue with the assignment, the assignment had to None, while if the send, then started, and then the first time (after return 4) execute, 7 first assigned to the res, then execute the next action, met next time the yield, return after the end of the results.

 

The program execution g.send (7), the program will continue to run from the line down the yield keyword, send the value 7 will be assigned to the variable res

6. Since the method comprising send next () method, the program execution will continue to run down the print method, and then re-enter the while loop

7. After the program execution encounters yield keyword again, will yield a return value back, pause the program again until the next method call or send method again.

 

 

 

This is the end, say something, why use this generator, because if the List, it will take up more space, for example, take 0,1,2,3,4,5,6 ...... ...... 1000

You might like this:

in n-Range for (1000):
A = n-
this time range (1000) to generate a default list 1000 contains a number, the memory accounted.

This time you can just use a combination of yield to be realized generator can be achieved by xrange (1000) this generator

yield combination:

def foo(num):
print("starting...")
while num<10:
num=num+1
yield num
for n in foo(0):
print(n)
输出:

starting...
1
2
3
4
5
6
7
8
9
10
 xrange(1000):

for the n-in xrange (1000):
A = the n-
 One should note that has no python3 when xrange (), and in python3 in, range () is xrange (), you can see the range in python3 in () types, it is already a <class 'range'> a, rather than a list, after all, this is to be optimized. 
---------------------
Author: Feng hearty
Source: CSDN
Original: https: //blog.csdn.net/mieleizhi0522/article/details/82142856
Copyright: This article is a blogger original article, reproduced, please attach Bowen link!

Guess you like

Origin www.cnblogs.com/jcjc/p/10936795.html