yield 的函數(shù)在 Python 中被稱之為 generator(生成器)。替代return函數(shù)的需要循環(huán)返回值使用的情況。
def fab(max):
a,b = 0,1
while a < max:
yield a
# print a
a, b = b, a+b
for n in fab(5):
print n
這里的fab()不是列表,不能直接print。
yield 的函數(shù)在 Python 中被稱之為 generator(生成器)。替代return函數(shù)的需要循環(huán)返回值使用的情況。
def fab(max):
a,b = 0,1
while a < max:
yield a
# print a
a, b = b, a+b
for n in fab(5):
print n
這里的fab()不是列表,不能直接print。