qiaoliang0302

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

迭代器和生成器

字符串、列表、元组、字典、集合、文件对象,这些都不是可迭代对象,只不过在for循环式,调用了它们内部的__iter__方法,把它们变成可迭代对象,然后for循环调用可迭代对象的__next__方法去取值,而且for循环会捕捉StopIteration异常,以终止迭代。

 

l=[1,2,3]
for i in l:
    print(i)
iter_l
=l.__iter__() print(iter_l.__next__()) print(iter_l.__next__()) print(iter_l.__next__())

 

posted on 2019-01-15 22:30  qiaoliang0302  阅读(69)  评论(0)    收藏  举报