- 迭代对象 :实现__iter__方法,返回迭代器。不需要显示继承Iterable,
- 迭代器: 实现_iter__方法,__next__方法,不需要显示继承Iterator
from collections import Iterable,Iterator
def generator():
for i in range(10):
yield i
st = ''
li = []
di = {}
ge = generator()
字符串,字典,列表,集合,生成器,都是可迭代对象
isinstance(st, Iterable), isinstance(li, Iterable), isinstance(di, Iterable), isinstance(ge, Iterable)
结果为:
(True, True, True, True)
字符串,字典,列表,集合都不是迭代体,生成器是迭代体
isinstance(st,Iterator), isinstance(li, Iterator), isinstance(di, Iterator), isinstance(ge, Iterator)
结果为:
(False, False, False, True)