# iterator from collections import iterablefrom collections import iterator# The data types that can directly act on for a loop have the following # 1. Collection data types, such as list, tuple, dict, set, SRT, and # 2.generator, including generators and yield generator function# these objects, which can be directly applied to a for loop, are called iterative objects: iterable# use Isinstance () to determine whether an object is a Iterable object print ( Isinstance ([], iterable)) print (Isinstance ({}, iterable)) print (Isinstance (' abc ', iterable)) print (Isinstance (X for x in range ()), iterable) print (Isinstance (100, iterable)) # The generator can not only be used for a for loop, but can also be continuously adjusted by the next () function and return the next value until the last throw Stopiteration error indicates that the next value cannot continue to be returned # can be next () The object called by the function call and continually returns the next value is known as an iterator:iterator# can use Isinstance () to determine whether an object is a Iterator object print (Isinstance (x for x in range ( iterator)) print (Isinstance ([], iterator)) Print (Isinstance ({}, Iterator) Print (isinstance (' abc ', iterator)) # generators are Iterator objects, but list, dict, and Str are iterable, but not iterator# The list, dict, str and other iterable into iterator can be usedITER () function print (Isinstance (ITER ([]), iterator)) print (Isinstance (ITER (' abc '), iterator)) # -------- -----------------------------------------------------------------------------------------------------# The following explains why the data types list, Dict, str, and so on are not iterator# because the Python iterator object represents a data stream, and the iterator object can be called by the next () function and continuously return to the next data. The Stopiterator error is thrown until there is no data # can think of this data stream as an ordered sequence, but we cannot know the length of the sequence in advance, we can only continue to calculate the next data on demand with the next () function # So the calculation of iterator is inert, and only when it is necessary to return the next data will it be calculated # iterator can even represent an infinitely large data stream, such as the whole natural number # and using list is never possible to store the whole natural number # - ------------------------------------------------------------------------------------------------------------# -------------------------------------------------------------------------------------------------------------# All objects that can be used for a for loop are iterable types # all objects that can be used for the next () function are iterator types, which represent a sequence of lazy computations # collection data types such as list, Dict, STR is iterable but not iterator, but a for loop that can get a iterator object # python through the ITER () function is essentially a # that is implemented by calling the next () function continuously--------- ----------------------------------------------------------------------------------------------------
Python---Iterator