python's yield --- "coroutine"

In programming, we often use the list, you need to declare and initialize a list of previously used in the large amount of data, they also need to complete the list produced by, for example, to store data for 1000, need to prepare a list of 1000's length, so that the computer it is necessary to prepare the list of memory placed in Python, while circulating mechanism for this calculation is known as a generator: generator, this comparison function to save space, the use of the method when using the list:

g=(i*2 for i in range(10))
data=g.__next__()
print(d)

When the list of supported data = g .__ next __ (), to generate a time only.

Application: the number of columns generated Feibolaqi

def fig(num):
    n,a,b=0,0,1
    while n<num:
        yield b
        a,b=b,a+b
        n+=1
    return 'done'

Fig (10) is defined as the length of columns 10

fig(10)
for i in range(10):
    try:
        x=next(g)
        print(x)
    except StopIteration as e:
        print('error %s' % e.value)
        break

The results:
. 1
. 1
2
. 3
. 5
. 8
13 is
21 is
34 is
55

Here, the most difficult to understand is the execution flow generator and function are not the same. Function execution order is encountered returnstatement or statement returns the last line of the function. Becomes a function generator, each call next()time of execution, encounters yieldstatement returns, execution again returned from the last yieldstatement continues execution.

Concurrent operation may also be implemented in the yield achieved by the effect of the case of a single thread

Import Time 

DEF Customer (name):
     Print ( ' % s for a bun ' % name)
     the while True: 
        Baozi = the yield 
        Print ( ' % s buns come, soon% s ate ' % (Baozi, name) )
     return  ' wrong '      # thrown 

DEF Producter (name, Student, NUM): 
    G = the Customer (Student)    # ** to bun 
    . G __next__ ()
     Print ( ' % s% s learned to cook bun and began baking ' %(name, Student))
     for i in (NUM) the Range: 
        the time.sleep ( 1)          # when 1 second 
        g.send ( ' leek stuffing ' )       # buns do, give students 
    Print ( ' buns sold out ' ) 

# Zhang cooks, students at noon today bun, made with 10 pounds of surface 20 buns 
Producter ( ' Zhang chef ' , ' student ' , 10)

operation result:

Students will bun
Zhang chef cooks learned to students bun, buns started
leek stuffing buns came, and soon the students eat
leek stuffing buns came, and soon the students eat
leek stuffing buns came, very students soon be eating
leek stuffing buns came, and soon the students eat
leek stuffing buns came, and soon the students eat
leek stuffing buns came, and soon the students eat
leek stuffing buns come , soon to be students eat
leek stuffing buns came, and soon the students eat
leek stuffing buns came, and soon the students eat
leek stuffing buns came, and soon the students ate
buns sold out

Guess you like

Origin www.cnblogs.com/linximf/p/11408681.html