''' 装饰器实现激活生成器 ''' def init(func): #传参得到 func = average def inner(*args,**kwargs): g = func(*args,**kwargs) #g = average() g得到一个生成器 g.__next__() # return g return inner @init #average = init(average) def average(): sum = 0 count = 0 avg = 0 while 1: num = yield avg sum+=num count+=1 avg = sum/count avg_g = average() ret =avg_g.send(10) print(ret)