生成器

字数 124阅读 38

一. 生成器的启动方式

协程的内部是用生成器实现的, 学好了生成器会更加理解协程的原理

#1. 生成器不只可以产出值,还可以接收值
def gen_func():
    #1. 可以产出值, 2. 可以接收值(调用方传递进来的值)
    html = yield "http://projectsedu.com"
    print(html)
    yield 2
    return "bobby"

if __name__ == "__main__":
    gen = gen_func()
    # 在调用send发送非none值之前,我们必须启动一次生成器, 方式有两种1. gen.send(None), 2. next(gen)
    url = gen.send(None)   # 启动生成器
    print(url)
    html = "bobby"
    print(gen.send(html))

# http://projectsedu.com
#  bobby
#  2

二. 生成器的close方法

这么做next(gen)会向 main 抛出StopIteration异常

def gen_func():
    yield "http://projectsedu.com"
    yield 2
    yield 3
    return "bobby"

if __name__ == "__main__":
    gen = gen_func()
    print(next(gen))
    gen.close()
    next(gen)
    print("bobby")

三. 生成器的throw方法

向上抛出异常

def gen_func():
    #1. 可以产出值, 2. 可以接收值(调用方传递进来的值)
    try:
        yield "http://projectsedu.com"
    except Exception as e:
        print('我出错了')
    yield 2
    yield 3
    return "bobby"

if __name__ == "__main__":
    gen = gen_func()
    print(next(gen))
    print(gen.throw(Exception, "download error")) # 向前一个yield抛出异常
    print(next(gen))
    gen.throw(Exception, "download error")   # yield3抛出异常

# http://projectsedu.com
# 我出错了
# 2
# 3

四. yield from 方法

#python3.3新加了yield from语法
from itertools import chain  # chain 和 my_chain有一样的功能

my_list = [1,2,3]
my_dict = {
    "bobby1":"http://projectsedu.com",
    "bobby2":"http://www.imooc.com",
}

def my_chain(*args, **kwargs):
    for my_iterable in args:
        yield from my_iterable
        # for value in my_iterable:
        #     yield value

for value in my_chain(my_list, my_dict, range(5,8)):
    print(value)
# 1
# 2
# 3
# bobby1
# bobby2
# 5
# 6
# 7

五. yield from 模仿协程示例

main 调用方, middle(委托生成器) sales_sum子生成器, yield from会在调用方与子生成器之间建立一个双向通道

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()
# start key: bobby牌面膜
# bobby牌面膜销量:  1200
# bobby牌面膜销量:  1500
# bobby牌面膜销量:  3000
# bobby牌面膜销量:  None
# bobby牌面膜销量统计完成!!.
# start key: bobby牌手机
# bobby牌手机销量:  28
# bobby牌手机销量:  55
# bobby牌手机销量:  98
# bobby牌手机销量:  108
# bobby牌手机销量:  None
# bobby牌手机销量统计完成!!.
# start key: bobby牌大衣
# bobby牌大衣销量:  280
# bobby牌大衣销量:  560
# bobby牌大衣销量:  778
# bobby牌大衣销量:  70
# bobby牌大衣销量:  None
# bobby牌大衣销量统计完成!!.
# final_result: {'bobby牌面膜': (5700, [1200, 1500, 3000]), 'bobby牌手机': (289, [28, 55, 98, 108]), 'bobby牌大衣': (1688, [280, 560, 778, 70])}

推荐阅读更多精彩内容