python之yield的用法

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_27378621/article/details/84886541

参见菜鸟教程示例:点击访问菜鸟示例

# -*- coding: UTF-8 -*-


def fab(max_num):
    i, a, b = 0, 0, 1
    while i < max_num:
        yield b  # 使用 yield
        # print b
        a, b = b, a + b
        i = i + 1


for n in fab(5):
    print n

大致作用相当于return内容的同时又不中断循环。(有时间再更)

猜你喜欢

转载自blog.csdn.net/qq_27378621/article/details/84886541
今日推荐