利用yield 实现Xrange功能

 1 def xrange(n):
 2     start = 0
 3     while True:
 4         if start>n:
 5             return
 6         yield start
 7         start+=1
 8 obj = xrange(5)
 9 n1 = obj.__next__()
10 n2 = obj.__next__()
11 n3 = obj.__next__()
12 n4 = obj.__next__()
13 n5 = obj.__next__()
14 n6 = obj.__next__()
15 n7 = obj.__next__()
16 
17 print(n1,n2,n3,n4,n5,n6,n7)

 

posted @ 2017-02-21 22:30  Erick-LONG  阅读(188)  评论(0编辑  收藏  举报