1. Can iterate:
When we print
Print (dir ([up])) can be seen in the results that appear with
' __iter__ ', this method, #次协议叫做可迭代协议
A function that contains the ' __iter__ ' method is an iterative function
strings, lists, tuples, dictionaries, and collections can all be used for loops, indicating that they are all iterative .
2. Iterator iterator
L = [1,2,3,4]l_iter= L.__iter__()#generation of Iteratorsitem = L_iter.__next__()#The use of iterators, with this method one by one values in the phishing iteratorPrintItem (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]li=l.__iter__() while True: try: Item=li.__next__() print(item) except stopiteration: #这里要输入的是报错的内容 , this error occurs when you perform the following
Print (' out of index ') Break
3. Determine if it is an iterator
Print('__next__' inchDir (range (12)))#see if ' __next__ ' is not internally __next__ after the range () method has been executed when he proves to be iterative,Print('__iter__' inchDir (range (12)))#see if ' __next__ ' is inside if the range () method executes __next__ when two results are true, the iterator fromCollectionsImportIteratorPrint(Isinstance (Range (100000000), Iterator))#Verify that the result obtained after range execution is not an iterator
4. Generator
If in some cases, we also need to save memory, we can only write ourselves. What we write ourselves is called the generator, which implements the function of the iterator.
The generators available in Python:
1. Generator function: general function definition , however, returns the result using the yield statement instead of the return statement. The yield statement returns one result at a time, in the middle of each result, suspends the state of the function so that the next time it leaves the execution
2. Generator expression: Similar to list derivation, however, the generator returns an object that produces results on demand, rather than building a list of results at a time
Generator generator:
Essence: iterators (so we have the __iter__ method and the __next__ method, we do not need to implement)
Features: Lazy operation, developer customization
Generator functions
A function that contains the yield keyword is a generator function. Yield can return a value from a function, but yield is different from Return,return's execution means the end of the program, the Call builder function does not get the specific value returned, but instead gets an iterative object. Each time you get the value of this iterative object, you can push the execution of the function to get a new return value. Until the function execution is finished.
def func (): #这就是一个简单的生成器函数 for in range : yield (' %s clothes produced '% i ' note the yield return data is used here instead of return J= Func ()print(J.__iter__())print(J.__iter__()) Print(J.__iter__())
Day13 python learning iterator, generator