Detailed usage python in yield - the simplest, clearest explanation (reprint)

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:

 def foo():
        print("starting...")
        while True:
            res = yield 4
            print("res:",res)
    g = foo()
    print(next(g))
    print("*"*20)
    print(next(g))

It's that simple few lines of code to let you know what is the output yield, this code:


    starting...
    4
    ********************
    res: None
    4

I direct the 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.

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

   

def foo():
        print("starting...")
        while True:
            res = yield 4
            print("res:",res)
    g = foo()
    print(next(g))
    print("*"*20)
    print(g.send(7))

An example of this look at the send function generator, the last line of this example took replaced the above example, the output:

    starting...
    4
    ********************
    res: 7
    4

Generally speaking first about the concept of send function: At this point you should note that the word purple above, and why the value is above that res None, this becomes a 7, in the end why, it is because, send a send a arguments to the res, as mentioned above, return time and did not assign res 4, when the next execution had to continue with the assignment operator, had the assignment to None, but if send it, started when, then the first time (after return 4) execute, 7 first assigned to the res, and then perform 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:

   

 for n in range(1000):
        a=n

This time range (1000) to generate a default list 1000 contains a number, so it is accounted for memory.

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)

Output:

    starting...
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
 

xrange(1000):

    for n in xrange(1000):
        a=n

 One should note that has no python3 when xrange (), and in python3 in, range () is xrange (), you can view python3 in range () type, which is already a <class 'range'> a, rather than a list, after all, this is to be optimized.

                                                                                                 thank you all

If you feel you have to help your appreciation is the biggest support for me!
----------------
Disclaimer: This article is CSDN blogger "von hearty 'original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source and this link statement.
Original link: https: //blog.csdn.net/mieleizhi0522/article/details/82142856

Published 49 original articles · won praise 11 · views 563

Guess you like

Origin blog.csdn.net/qq_45769063/article/details/105117295