Python

可以被next函数调用,并不断返回下一个值的对象

迭代器与可迭代对象

  1. 特点
    • 只能前进,不能后退
  2. 可迭代对象不等于迭代器
    • 因为可迭代对象不一定被next函数调用(生成器是可迭代的,所以是迭代器)
  3. collections.abc
    • 容器的抽象基类
    1. Iterable
      • 可迭代对象
  4. 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