标签:top __iter__ obj ati 方法 ble for 循环 rabl next
生成器都是迭代器,迭代器不一定是生成器
list,tuple,dict,string:Iterable(可迭代对象)
l = [1,2,3,4]
d = iter(l) # l.__iter__()
print(d) #<list_iterator object at 0x0000000000687B38>
print(next(d))
print(next(d))
print(next(d))
print(next(d))
print(next(d)) #StopIteration
什么是迭代器?
满足两个条件:1 有iter方法 2 有next方法
for 循环内部三件事
1 调用可迭代对象的iteration方法返回一个迭代器对象
2 不断调用迭代器对象的next方法
3 处理StopIteration
for i in [1,2,3,4]:
iter([1,2,3,4])
标签:top __iter__ obj ati 方法 ble for 循环 rabl next
原文地址:http://www.cnblogs.com/y-m-f/p/7798553.html