def init(func): def wrapper(*args,**kwargs): obj = func(*args,**kwargs) next(obj) return obj return wrapper @init def food(name): print("%s start to eat" % name) food_list = [] while True: food = yield food_list food_list.append(food) print("%s eat %s" % (name,food_list)) e = food("xyp") # xyp start to eat e.send("apple") # xyp eat ['apple'] e.send("apple") # xyp eat ['apple', 'apple'] e.send("apple") # xyp eat ['apple', 'apple', 'apple'] ......
相关文章: