Python3 迭代器

 1 '''
 2 生成器都是迭代器,迭代器不一定是生成器
 3 '''
 4 
 5 # list1 = [1,2,3,4,5]
 6 # p1 = iter(list1)        # 等价于__iter__()
 7 # print(p1)
 8 # print(next(p1))
 9 # print(next(p1))
10 # print(next(p1))
11 # print(next(p1))
12 
13 
14 '''
15 迭代器必须满足下列两个条件:
16 1.有iter方法
17 2.有next方法
18 '''
19 
20 '''
21 for循环内部的三件事:
22 1.调用可迭代对象的iter方法,返回一个迭代器对象
23 2.调用迭代器对象的next方法
24 3.处理StopIteration
25 '''
26 
27 # for i in [5,6,7]:
28 #     iter([5,6,7])
29 
30 # from collections import Iterator,Iterable       # 迭代器,迭代对象
31 # print(isinstance([3,4],list))       # 判断前一个参数是不是后边参数写出的type
32 # print(isinstance(6,list))           # 一样是True,否则是False
33 # 
34 # 
35 # list2 = [1,2,3,4,5,6,7,8,9,10]
36 # p2 = iter(list2)
37 # print(p2)
38 # print(isinstance(list2,list))       # 是否是列表
39 # print(isinstance(list2,Iterable))   # 是否是迭代对象
40 # print(isinstance(list2,Iterator))   # 是否是迭代器
41 # print(isinstance(p2,Iterator))      # 是否是迭代器
原文地址:https://www.cnblogs.com/Infi-chu/p/7684668.html