yield的语法规则是:在yield这里暂停函数的执行,并返回yield后面表达式的值(默认为None),直到被next()方法再次调用时,从上次暂停的yield代码处继续往下执行。
当没有可以继续next()的时候,抛出异常,该异常可被for循环处理。
>>> def fib(n):
a, b = 0, 1
i = 0
while i < n:
yield b
a, b = b, a+b
i += 1
>>> f = fib(5)
>>> next(f)
1
>>> next(f)
1
>>> next(f)
2
>>> next(f)
3
>>> next(f)
5
>>> next(f)
Traceback (most recent call last):
File "<pyshell#9>", line 1, in <module>
next(f)
StopIteration