Python Generator Generator

Source: Internet
Author: User

Prerequisites: If the function contains yield is the generator, the execution process encountered yield to jump out.

Example:
Def gen ():
For I in range (10):
x = Yield I
Print (x)
G=ge ()
Print (G.send (None)))
Print (G.send (2))

First say expression x = yield I
If this expression is only x = i, I believe everyone can understand. The value of I is assigned to X, and now the right side of the equals sign is a yield I, so I have to execute yield I first and then the assignment.
Because the generator jumps out when it encounters yield, yield returns the I value to the caller.
Next action for this expression: assignment. But because the yield on the right side of the equals sign is paused, in other words x = yield I executes half, when the caller goes back to the generator function by Send (VAR) back to the previous assignment expression is paused there, so next execution x = yield I the other half, that is the assignment operation, This value is the value that the caller sends to the generator via Send (VAR).

Another example:
DEF consumer ():
r = "
While True:
n = yield R
If not n:
Return
Print (' [CONSUMER] consuming%s ... '% n)
r = ' OK '

def produce (c):
C.send (None)
n = 0
While n < 5:
n = n + 1
Print (' [PRODUCER] producing%s ... '% n)
r = C.send (n)
Print (' [PRODUCER] Consumer return:%s '% r)
C.close ()

c = Consumer ()
Produce (c)
Execution Result:
[PRODUCER] Producing 1 ...
[CONSUMER] Consuming 1 ...
[PRODUCER] Consumer return:200 OK
[PRODUCER] Producing 2 ...
[CONSUMER] Consuming 2 ...
[PRODUCER] Consumer return:200 OK
[PRODUCER] Producing 3 ...
[CONSUMER] Consuming 3 ...
[PRODUCER] Consumer return:200 OK
[PRODUCER] Producing 4 ...
[CONSUMER] Consuming 4 ...
[PRODUCER] Consumer return:200 OK
[PRODUCER] Producing 5 ...
[CONSUMER] Consuming 5 ...
[PRODUCER] Consumer return:200 OK

Official Note:
Notice that the consumer function is a generator that puts a consumer into the produce after:
First Call C.send (None) to start the generator, and then, once you have produced something, switch to consumer execution by C.send (n); Consumer yields the message, handles it, and passes the result back through yield. Produce get consumer processing results, continue to produce the next message; Produce decided not to produce, through C.close () close consumer, the entire process is over.

My understanding:
Note that the first place of the produce () function is C.send (None), as stated above, which is to initialize the generator, and make the initial yield (which is returned when the consumer function is encountered), which is a value of "R", If you execute the command line, you will find that the result is empty first, that is, his credit. Then to N=0+1=1, at which point the C.send (1) is executed, it begins with the assignment of n in the CONSUMER function, that is, [CONSUMER] consuming 1, and continues executing the command, at which time R is assigned ' OK ' because while True will always loop , so continue again, but the generator encounters yield will automatically jump out, at this time out of the result becomes r= ' OK '.
Additional knowledge:
While True:
You can jump out of a loop only if you encounter continue and break. It is also possible to meet yield in the generator.
If not x: equivalent if x is not none if not X is None '
None in Python, false, empty string "", 0, empty list [], empty Dictionary {}, empty tuple () equals false

The reference address is as follows:
Python
Liu Xuefeng

Python Generator Generator

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.