生成器

时间:2018-09-13 17:12:30   收藏:0   阅读:114
def fib(num):
    a, b = 0, 1
    index = 0
    while index < num:
        yield a
        a, b = b, a+b
        index += 1
    return ok...

f = fib(100)

while True:
    try:
        print(next(f))
    except Exception as e:
        print(e.value)
        break
        
# for num in f:
#     print(num)

 

def fib(num):
    a, b = 0, 1
    index = 0
    while index < num:
        temp = yield a
        print(temp:,temp)
        a, b = b, a+b
        index += 1
f = fib(20)
ret = next(f)
print(ret)
ret2 = f.send(test)
print(ret2)

 

原文:https://www.cnblogs.com/kuraki/p/9641568.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!