Generator concurrent processing is actually the switching of functions

#Generator concurrent processing is actually the switching of functions 
# yield is equivalent to return control is the return value of the function 
# x=yield accepts the value passed by send and pays it to x 
def test():
     print ( ' start ' )
    first = yield 1 # return 1, yield accepts a value to pass to first=None, if there is no 1, it returns none 
    print ( ' first ' ,first) # first value is that sentence 
    yield 2
     print ( ' second times ' )
t=test()
print(t)
res =t .__ next__ ()
 print (res)
 # t.__next__() 
ret=t.send( ' The function stays at the first position, I am assigning the value to first ' ) #Trigger generator execution and t._next_ result same 
print (ret)

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325339477&siteId=291194637