Python
可以被next函数调用,并不断返回下一个值的对象
迭代器与可迭代对象
- 特点
- 只能前进,不能后退
- 可迭代对象不等于迭代器
- 因为可迭代对象不一定被next函数调用(生成器是可迭代的,所以是迭代器)
- collections.abc
- 容器的抽象基类
- Iterable
- 可迭代对象
- iter
- 可迭代对象转迭代器
代码示例:
# 判断一个对象是否可迭代:
import collections.abc
foo = [0,7,2,1]
bar = isinstance(foo,collections.abc.Iterable)
print(bar) # True
foo = '123456'
bar = iter(foo)
print(next(bar)) # 1