迭代器

#可用于for循环的对象——可迭代对象Iterable:list,tuple,dict,str,generator
#可被next()调用并不断返回下一个值的对象——迭代器Iterator:generator
from collections import Iterable
from collections import Iterator

print(isinstance({},Iterable))
print(isinstance([],Iterable))
print(isinstance((),Iterable))
print(isinstance("ab",Iterable))

print('------------------------')
print(isinstance({},Iterator))
print(isinstance("ab",Iterator))
print(isinstance((),Iterator))
print(isinstance([],Iterator))
print(isinstance((i for i in range(5)),Iterator))
print('------------------------')

a=[1,2,3,4,6,7]
b=iter(a)
print(isinstance(b,Iterator))

with open('hello','r',encoding='utf-8') as f:
for line in f: #用到的也是iterator的机理
print(line)
posted @ 2018-01-25 14:23  cecelia  阅读(110)  评论(0编辑  收藏  举报