生成器的应用
# def average(): # sum = 0 # count = 0 # avg = 0 # while True: # num = yield avg # sum += num # 10 # count += 1 # 1 # avg = sum/count # # avg_g = average() # avg_g.__next__() # avg1 = avg_g.send(10) # avg1 = avg_g.send(20) # print(avg1)
# def init(func): #装饰器 # def inner(*args,**kwargs): # g = func(*args,**kwargs) #g = average() # g.__next__() # return g # return inner # # @init # def average(): # sum = 0 # count = 0 # avg = 0 # while True: # num = yield avg # sum += num # 10 # count += 1 # 1 # avg = sum/count # # avg_g = average() #===> inner # ret = avg_g.send(10) # print(ret) # ret = avg_g.send(20) # print(ret)
yield from 功能
#python 3中 # def generator(): # a = 'abcde' # b = '12345' # for i in a: # yield i # for i in b: # yield i # def generator(): # a = 'abcde' # b = '12345' # yield from a # yield from b # # g = generator() # for i in g: # print(i)