22 生成器进阶

Posted on 2018-09-16 14:55  白色雪狐  阅读(141)  评论(0编辑  收藏  举报
def generator():
    print(123)
    yield 1
    print(456)
    yield 2

g = generator()
ret = g.__next__()
print('***',ret)
ret = g.send(None)    #效果和__next__一样
print('***',ret)
# 123
# *** 1
# 456
# *** 2
def generator():
    print(123)
    content = yield 1
    print('----',content)
    print(456)
    yield 2

g = generator()
ret = g.__next__()
print('***',ret)
ret = g.send('hello')    #send 可以传值
print('***',ret)
# 123
# *** 1
# ---- hello
# 456
# *** 2
#send 获取下一个值的效果和next基本一致
#只是在获取下一个值的时候,给上一yield的位置传递一个数据
#使用send的注意事项
# 第一次使用生成器的时候 是用next获取下一个值
# 最后一个yield不能接受外部的值
# 获取移动平均值
#10 20 30 10
#10 15 20 17.5
#avg = sum/count
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)
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)
# send
# send的作用范围和next一模一样
# 第一次不能用send
# 函数中的最后一个yield不能接受新的值
#
# 计算移动平均值的例子
# 预激生成器的装饰器的例子
# yield from

Copyright © 2024 白色雪狐
Powered by .NET 9.0 on Kubernetes