Python yield关键字测试

yield关键字用来定义生成器(Generator)
#!/bin/python3
#-*- coding: utf-8 -*-
import random
import math, sys

def p(x): print(x)
def pt(x): print(x, type(x))

for line in open("test.py"):   
    # print(line, end="")
    pass

def fab(max):
    a,b = 1,1
    while a < max:
        yield a
        a, b = b, a+b
    return 1
it = fab(2)

for i in fab(1000):
    print(i, end=" ")


发表于:2014-10-13 13:59:37

原文链接(转载请保留): http://www.multisilicon.com/blog/a25306834.html

友情链接: MICROIC
首页