python 生成器
g = [x for x in range(3)] # 列表生成式
gg = [x * x for x in range(1,100) if x % 2 == 0]
G = (x for x in range(3)) # 生成器
print type(g),type(G)
# 输出
<type 'list'> <type 'generator'>
# 斐波那契数列
def fib(max):
n, a, b = 0, 0, 1
while n < max:
# print b
yield b
a, b = b, a + b # 循环一次后后一个数等于其后面2个数之和,即 a = b ,b = a + b
# print b
n = n + 1 # 循环次数,循环到指定次数跳出死循环
# print n
fib(6)