Python

python中的yield

2017-09-23  本文已影响6人  踏云小子

show me the code

def fab(max): 
    n, a, b = 0, 0, 1 
    while n < max: 
        yield b 
        # print b 
        a, b = b, a + b 
        n = n + 1 
        ...


>>> for n in fab(5): 
...     print n 
... 
1 
1 
2 
3 
5

yield一般用在用循环功能的函数内,用在把循环里的数据提取出来,有点像OC的block

上一篇 下一篇

猜你喜欢

热点阅读