[Python] python里的generator函数 →→→→→进入此内容的聊天室

来自 , 2019-03-26, 写在 Python, 查看 130 次.
URL http://www.code666.cn/view/717d8b3d
  1. #basic syntax(a function that yield)
  2. def genMul2(N):
  3.   for i in range(N):
  4.    yield i * 2
  5. for i in genMul2(5):print i
  6. #inside for, the next method is called
  7. #在for循环的内部,Python调用了next方法。
  8. #下面的x叫做generator对象
  9. x = genMul2(2)
  10. #一直调用next方法,最后会抛出一个异常
  11. print x
  12. print x.next()
  13. print x.next()
  14. #print x.next()  !!!!error!!!!StopIteration
  15.  
  16.  
  17. #//python/4451

回复 "python里的generator函数"

这儿你可以回复上面这条便签

captcha