# Generator function # DEF Generator (): # Print (. 1) # return 'A' # # RET = Generator () # Print (RET) # function as long as the yield keyword is contained generator function # yield can not return common and need to write the function # DEF generator (): # Print (. 1) # the yield 'a' # # generator function: get a generator after the execution as a return value # RET = generator () # Print (RET) # Print (RET .__ Next __ ()) # DEF Generator (): # Print (. 1) # the yield 'A' # Print (2) # yield 'b' # yield 'c' # g = generator() # for i in g: # print(i) # ret = g.__next__() # print(ret) # ret = g.__next__() # print(ret) # ret = g.__next__() # print(ret) #娃哈哈%i def wahaha(): for i in range(2000000): yield '娃哈哈%s'%i # g = wahaha() # g1 = wahaha() # print(g.__next__()) # print(g1.__next__()) # g = wahaha() # count = 0 # for i in g: # count +=1 # print(i) # if count > 50: # break # # print('*******',g.__next__()) # for i in g: # count +=1 # print(i) # if count > 100: # break
Monitor the input file
def tail(filename): f = open(filename,encoding='utf-8') while True: line = f.readline() if line.strip(): yield line.strip() g = tail('file') for i in g: if 'python' in i: print('***',i)