Skip to content

Instantly share code, notes, and snippets.

@python012
Created December 9, 2014 02:31
Show Gist options
  • Save python012/81e159df93665c465005 to your computer and use it in GitHub Desktop.
Save python012/81e159df93665c465005 to your computer and use it in GitHub Desktop.
Use yield in Fibonacci list
#!/usr/bin/env python
# -*- coding:utf-8 -*-
def fab(max):
n, a, b = 0, 0, 1
while n < max:
yield b
a, b = b, a + b
n = n + 1
for n in fab(5):
print n
@python012
Copy link
Author

"yield 的作用就是把一个函数变成一个 generator,带有 yield 的函数不再是一个普通函数,Python 解释器会将其视为一个 generator,调用 fab(5) 不会执行 fab 函数,而是返回一个 iterable 对象!在 for 循环执行时,每次循环都会执行 fab 函数内部的代码,执行到 yield b 时,fab 函数就返回一个迭代值,下次迭代时,代码从 yield b 的下一条语句继续执行,而函数的本地变量看起来和上次中断执行前是完全一样的,于是函数继续执行,直到再次遇到 yield。"

http://blog.csdn.net/preterhuman_peak/article/details/40615201

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment