iterator_iterable_generator

[Iterable object] saves the data that has been generated and takes up a lot of space . The
__iter__ method is an iterable object (Iterable)

[Iterator] saves the method of generating data, takes up a very small space, and returns data when needed
. There are both __iter__ and __next__, which is iterator (Iterator)

[Generator] is a [special] iterator that saves the method of generating data
(the sign is [yiled] in def, and the def with yield is not a function (function)
The generator's send (parameter) can be woken up Return parameters (requires variable reception),

Iterable object (iterable): The data types that can be used to traverse the contents of the for...in...
include: [list, tuple, dict, set, str] (collection data type), [generator\list generation \iterator]

Iterator (Iterator): data type that can be queried by next() The calculation of Iterator is lazy, it will only calculate when it needs to return the next data.
Contains: generator, generator function

    The essence of for...in   ...
loop Iterate the iterator of the object,     2.2 Then through the iterator's next() function, call the iterator's __next__ method to return the data     2.3 Until the exception StopIteration occurs, exit the iterative for...in... process. 1.2 No error TypeError: 'xxx' object is not iterable





 

Judgment method:
from collections import Iterable #Call Iterable in the collections module #Example 
:
isinstance('abc', Iterable) # Whether str is iterable
True
isinstance([1,2,3], Iterable) # Whether list is iterable
True
isinstance (123, Iterable) # Whether the integer is iterable
False

Summarize:

1. An iterator must be an iterable object, and an iterator object is not necessarily an iterator (list, etc.)

2. A generator must be an iterator, and a special kind of iterator is a generator

3. Using for...in... to traverse the iterative object is the most common way

Guess you like

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