python3.3 新加的yield from 语句
1.yield from
def my_chain(*args, **kwargs): for my_iterable in args: yield from my_iterable # yield from 相当于下面的: # for value in my_iterable: # yield value
2.yield from 会把值循环遍历
def g1(iterable): yield iterable def g2(iterable): yield from iterable for value in g1(range(5)): print(value) for value in g2(range(5)): print(value) # 结果 range(0, 5) 0 1 2 3 4
2.yield from 主动处理StopIteration异常
final_result = {} def sales_sum(pro_name): total = 0 nums = [] while True: x = yield print(pro_name + "销量: ", x) if not x: break total += x nums.append(x) return total, nums def middle(key): while True: final_result[key] = yield from sales_sum(key) print(key + "销量统计完成!!.") def main(): data_sets = { "bobby牌面膜": [1200, 1500, 3000], "bobby牌手机": [28, 55, 98, 108], "bobby牌大衣": [280, 560, 778, 70], } for key, data_set in data_sets.items(): print("start key:", key) m = middle(key) m.send(None) # 预激middle协程 for value in data_set: m.send(value) # 给协程传递每一组的值 m.send(None) print("final_result:", final_result) if __name__ == '__main__': main() def sales_sum(pro_name): total = 0 nums = [] while True: x = yield print(pro_name + "销量: ", x) if not x: break total += x nums.append(x) return total, nums if __name__ == "__main__": my_gen = sales_sum("bobby牌手机") my_gen.send(None) my_gen.send(1200) my_gen.send(1500) my_gen.send(3000) try: my_gen.send(None) except StopIteration as e: result = e.value print(result)