生成器的惰性机制

 1 def func():
 2     print(111)
 3     yield  1
 4 g = func()
 5 g1 = (i  for i in  g)
 6 g2 = (i  for i in  g1)
 7 
 8 print(list(g))   # 111 [1]
 9 print(list(g1))  # []
10 print(list(g2))  # []
11 
12 print(next(g))   # 111 1
13 print(next(g1))  # StopIteration
14 print(next(g2))  # StopIteration

 

posted @ 2018-07-19 09:09  Niuli'blog  阅读(396)  评论(0编辑  收藏  举报