What exactly are iterator, iterable, and iteration?

前端 未结 13 1689
遥遥无期
遥遥无期 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 05:49

    In Python everything is an object. When an object is said to be iterable, it means that you can step through (i.e. iterate) the object as a collection.

    Arrays for example are iterable. You can step through them with a for loop, and go from index 0 to index n, n being the length of the array object minus 1.

    Dictionaries (pairs of key/value, also called associative arrays) are also iterable. You can step through their keys.

    Obviously the objects which are not collections are not iterable. A bool object for example only have one value, True or False. It is not iterable (it wouldn't make sense that it's an iterable object).

    Read more. http://www.lepus.org.uk/ref/companion/Iterator.xml

提交回复
热议问题