---restore content starts---
Iterator Generator iterator: 1, what is an iterator protocol
①dir (measured object) if it contains __iter__, then this object is called an iterative object and follows an iterative protocol.
② Iterate objects:iterable can transform iterators (how to: Define a __iter__ () method as internal)
Iterators (iterator) follow the iterator protocol
2, iterator
1, only the data containing the __iter__ method is the object of the class iteration
L = [1,2,3,4]print('__iter__' in Dir (l))
2, the data containing the __ITER__ method and containing the __next__ method is an iterator.
Print (dir ([up]. ) __iter__()))
3, the meaning of iterators:
① iterators Save Memory
② iterators have an inert mechanism
The ③ iterator cannot be repeated and is executed down.
4, for loop mechanism
For can be looped with: strings, lists, meta-ancestors, dictionaries, and collections, indicating that they can all be iterated.
In the For loop, the __next__ method is called internally to fetch a value of one.
Then we'll use the next method of the iterator to write a traversal that doesn't depend on for.
L = [1,2,3,4]l_iter= L.__iter__() Item= L_iter.__next__()PrintItem (item)= L_iter.__next__()PrintItem (item)= L_iter.__next__()PrintItem (item)= L_iter.__next__()PrintItem (item)= L_iter.__next__()Print(item)
This is an error code, if we always take next to the iterator has no elements, we will throw an exception stopiteration, tell us that there is no valid element in the list.
At this point, we're going to use the exception handling mechanism to get rid of this exception.
L = [1,2,3,4= L.__iter__() while True: try: = L_iter. __next__ () print(item) except stopiteration: Break
So now we're going to use the while loop to do what the original for loop does, and who do we get a value from? Is that l_iter? Well, this l_iter is an iterator.
1, internal contains the __iter__ method, he will iterate the object first into an iterator, and then call the __next__ method.
2, it has an exception handling method
range It is an iterative object, not an iterator
Verify that range is not an iterator
Print('__next__' inchDir (range (12)))#see if ' __next__ ' is internally __next__ after the range () method has been executedPrint('__iter__' inchDir (range (12)))#see if ' __next__ ' is internally __next__ after the range () method has been executed
From collections Import iterable fromCollectionsImportIterator
Print (Isinstance (range (100000000), iterable))Print(Isinstance (Range (100000000), Iterator))#Verify that the result obtained after range execution is not an iterator
Generator: 1, the essence of the generator is an iterator, an iterator, and the generator is Python's own iterator written in Python code
2, Generator features: ① can be written with generator function
② can build iterators with various derivations
③ can be transformed by data
The difference between 3,return and yield
①return returns to the caller value and ends the function
②yiled returns to the caller value and rests the pointer at the current position
4, send:
1, send a value to the previous yiled overall
2, send cannot give a value to the last yiled
3, when you get the first value, you cannot use send only with Next
---restore content ends---
Python path-function iteration, generator