yield Usage


Reproduced original link: https: //blog.csdn.net/u011318077/article/details/93749143

Python3 in yield for beginners has been a difficult existence, many online tutorials, crackling write a lot, also cited many examples, but after reading still do not know why, the following questions still do not know how to answer, content a bit more in some places may a little long-winded, but they are full of dry goods.

yield what is doing?
yield is how to implement it?
What good is the yield?
article structure

1. Iterative with iterables

2. yield a simple case and the steps

3. yield the send function

What are the benefits 4. yield is

1. Iterative with iterables

Before you begin, first understand what an iterator and iterable may, in fact, because yield is a special iterator, but this iterator more elegant.

Iterables

# Iterables: Example list of
S = 'the ABC'
L = List (S)
Print (L)
. 1
2
. 3
. 4
[ 'A', 'B', 'C']
. 1
iterator

# Iterator object L1
S = 'the ABC'
L = List (S)
L1 = ITER (L)
Print (L1)
# value taken iterator vessel, no exception is thrown value after
Print (Next (L1))
Print (Next (L1))
Print (Next (L1))
Print (Next (L1))
. 1
2
. 3
. 4
. 5
. 6
. 7
. 8
. 9
10
<list_iterator Object AT 0x0000020D793D95C0>
A
B
C
the StopIteration
. 1
2
. 3
. 4
. 5
above case l is a list is an iterable
l1 is an iterator, direct printing, the result is <list_iterator object at 0x0000020D793D95C0>, the value of which access can be used for cycles or next function, after all values have been visited, and finally throws StopIteration abnormal

About iterator and iterable refer to my another blog post, which has a detailed explanation:
https://blog.csdn.net/u011318077/article/details/93754013

yield Builder is an elegant iterator, access will be used next function, it can more easily understand the implementation process and the principles of yield generator after understanding the iterator.

2. yield a simple case and the steps

Following entered, if you do not already have a preliminary division understanding of yield, then you should first yield seen as a "return",
this is intuitive, it is first and foremost a return, normal return is what is meant is to return some in the program values, and then return to the program is no longer running down. After seen as a return then it is seen as part of a generator (generator) of
(real function with a yield of iterators), well, if you do not understand these, then seen as the first yield return, then look at the following program directly, you will understand the full meaning of the yield (just as the first return, backward-looking nature will understand).

Look at a normal function
# a common function:
DEF foo ():
Print ( 'Starting .....')
# calling function, directly execute the statement
G = foo ()
Print ( "*" * 100)
. 1
2
. 3
. 4
5
6
Starting .....
****************************************** **********
1
2
generator function
# include the yield keyword, it becomes a generator function
# call the function does not execute the statement
DEF foo ():
Print ( 'Starting ..... ')
the while True:
RES = the yield. 4
Print ( "RES:", RES)

# The following function call is not performed, the latter can first statement commented
# progressive operation codes observed effects
G = foo ()
Print ( "Results of the first call:")
Print (Next (G))
Print ( " * "* 100)

print ( "execution result of the second call:")
Print (Next (G))
print ( "*" * 100)
. 1
2
. 3
. 4
. 5
. 6
. 7
. 8
. 9
10
. 11
12 is
13 is
14
15
16
. 17
18 is
first called for execution The results:
Starting .....
4
***************************************** ***************************
second call to the results:
RES: None
4
************ ************************************************** ******
. 1
2
. 3
. 4
. 5
. 6
. 7
. 8
will be explained operation of the code sequence, corresponding to stepping through code ():
after the start of program execution, since there is the function foo yield keyword, and it is not really a function foo implementation,
But first get a generator g (equivalent to an object), a state function, pause function equivalent to the
first call to execute until the next method, function foo officially started, perform the print function foo the method then enters the while loop
after the program encounters the yield keyword, then the yield to think about the return, return a 4, the program stops,
but the program simply returns a value of 4, 4 and did not perform the operation assigned to res, At this time next (g) statement is executed,
so the results of the first call after the two rows (the first print of the result of the above while the second is the result of return)
is performed print (next (g )) before calling the function, and finally print out the return value of 4
program execution print ( "" * 100), the output 100
to perform the second call, they began to perform the following print (next (g)), and on top of that this time similar, but the difference is, this time from the next place to just stop the program started, which is to be executed res of the assignment statement,
this time to pay attention, yield 4 return value after 4 stops, no There are assigned to the front of the res, (because that is just out of the return, and not left to the assignment of transmission parameters), then the code is actually from print ( "res:", res ) started,
this time the assignment is res empty, it is None, so then the following output is res: None,
the program will continue to execute while there, met again yield, this time to return the same 4, then the program stops, the return is 4 out of print output function 4.
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.
to sum up

The above foo () is a function generator, a generator function call when the yield, "state" of the generator function is frozen, all the values ​​of the variables are retained, the position of the next line of code will be executed is recorded, this line of code is the yield of the end position again until you call next (). Once the next () is called again, the function generator will start from where it left off. If you never call next (), yield saved state was ignored.

generator is used to generate a series of values, yield is like a generator function returns the result of, (yield also appears to return), yield only other thing to do is to save the state of a function generator

The difference between the yield and return, execution will continue after the return of the back, but the code will stop after the yield continue, note that only the interior of the generator function stop code generator function unaffected external code

Similar generator is a special type of iterator (Iterator) and iterator, we can get the next value from the generator by using the next ()

Guess you like

Origin www.cnblogs.com/happytaiyang/p/11618461.html