generator function

 

s = (i for i in range(10))
print(s)
# <generator object <genexpr> at 0x0000028C206AB0F8>Generator object
#Generator function must have yield 
def test():
    yield 1
    yield 2
    yield 3

res=test()
print(res)
print(res.__next__())#The generator must have the next method to correctly produce output
def test():
     print ( ' Starting to have children -== ' )
     print ( ' Happy---- ' )
     print ( ' Happy--- ' )
     yield  ' I ' # Don't continue the following print after yield is executed 
    print ( ' starting to have children ' )
     yield  ' daughter ' 
    print ( ' starting to have a daughter ' )
     yield  ' granddaughter '

res = test()
 print (res)
 print (res. __next__ ()) #The generator must have the next method to produce the output correctly 
# print(res.__next__())#The generator must have the next method to produce the output correctly 
# print(res.__next__())#The generator must have the next method to correctly generate output

 

Guess you like

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