Originally in the sketch generator, but the generator is a special kind of iterator, so I think the iterator should be written first.
What is an iteration
An object that can directly act on a for loop is called an iterative object (iterable).
An object that can be called by the next () function and continually returns the next value is called an iterator (Iterator).
All iterable can be converted to iterator via the built-in function iter ()
Iteration Type :
1,for loop: Used for any object that can be iterated, in fact, in Python, the iterative tools that scan objects from left to right, and so on, include for loops, list parsing, in-member relationship testing, and map built-in functions.
2. File iterator: Because the file object is its own iterator, the advantage is: (Reduce the use of memory, do not need to load the content into memory)
3, Manual iteration: ITER and Next
At the start of the For loop, it is passed to the ITER built-in function to get a hit from an iterative object, and the returned object contains the required next method (this step is not necessary for the file because the file object is its own iterator)
For example:
#文件 >>> Import tab>>> f=open ("test", "R") >>> ITER (f) is ftrue# sequence string >>> a=[1,2,3,4] >>> ITER (a) is Afalse
Liefs
This article is from the "Wang Xiao Acid" blog, please be sure to keep this source http://wangaimin.blog.51cto.com/8499946/1908361
python-iterator detailed