What exactly are iterator, iterable, and iteration?

前端 未结 13 1692
遥遥无期
遥遥无期 2020-11-21 05:27

What is the most basic definition of \"iterable\", \"iterator\" and \"iteration\" in Python?

I have read multiple definitions but I am unable to identify the exact m

13条回答
  •  無奈伤痛
    2020-11-21 06:00

    Iterables have a __iter__ method that instantiates a new iterator every time.

    Iterators implement a __next__ method that returns individual items, and a __iter__ method that returns self .

    Therefore, iterators are also iterable, but iterables are not iterators.

    Luciano Ramalho, Fluent Python.

提交回复
热议问题