Python Basics: Iterators

iterator

Objects that can act directly on for loops are collectively called iterable objects: Iterable, such as: list, tuple, dict, set, str, generator, including generator functions with yield; you can use isinstance() to determine whether an object is iterable Object:

>>> from collections import Iterable
>>> isinstance([], Iterable)
True
>>> isinstance({}, Iterable)
True
>>> isinstance('abc', Iterable)
True
>>> isinstance((x for x in range(10)), Iterable)
True
>>> isinstance(100, Iterable)
False

The generator can not only act on the for loop, but also can be continuously called by the next() function and return the next value, until the StopIteration error is thrown at the end, indicating that it cannot continue to return the next value.
Objects that can be called by the next() function and continue to return the next value are called iterators: Iterators
can use the isinstance() function to determine whether an object is an Iterator object:

>>> from collections import Iterator
>>> isinstance((x for x in range(10)), Iterator)
True
>>> isinstance([], Iterator)
False
>>> isinstance({}, Iterator)
False
>>> isinstance('abc', Iterator)
False

We may be wondering, why list, dict, str and other data types are not Iterators? At this time, because Python's Iterator object represents a data stream, the Iterator object can be called by the next() function and returns the next data continuously until there is no data, and a StopIteration error is thrown. This data stream can be regarded as an ordered sequence, but we cannot know the length of the sequence in advance, and can only continuously calculate the next data on demand through the next() function, so the calculation of the Iterator is lazy, only when needed. It will not be calculated until the next data is returned. Iterator can even represent an infinite data stream, such as all natural numbers, and it is never possible to store all natural numbers using list.

Summarize:

  • Any object that can act on a for loop is an Iterable object;
  • Any object that can act on the next() function is an Iterator object, which represents a sequence of lazy calculations;

Collection data types like: list, dict, str, etc. are iterable objects, but not iterator objects. However, an iterator object can be obtained through the iter() function. Python's for loop is essentially implemented by continuously calling the next() function, for example:

for x in [1, 2, 3, 4, 5]:
    pass

In fact, it is completely equivalent to:

# 首先获得Iterator对象:
it = iter([1, 2, 3, 4, 5])
# 循环:
while True:
    try:
        # 获得下一个值:
        x = next(it)
    except StopIteration:
        # 遇到StopIteration就退出循环
        break

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324692031&siteId=291194637