One, iterator
1, the object can be iterated: the object that can directly act on the For loop is called an iterative object, namely iterable.
Common Iteration objects: str, list, tuple, dict, set, range
Methods for detecting iterative objects:
1), detection method: Dir (the object being measured)-if it contains __iter__, then this object is called an iterative object.
#返回True表示是可可迭代对象, false is not str1 = ' Hello ' print (' __iter__ ' in Dir (str1)) #Truenum = 2018print (' __iter__ ' in Dir (num)) #False
2), test whether it is an iterator or an iterative object
#检测可迭代对象与迭代器的方法lis = [1,2,3]from collections import iterable #Iterable can iterate objects from collections import Iterator # Iterator iterators
Print (Isinstance (lis,iterable)) #Trueprint (Isinstance (lis,iterator)) #False # isinstance () is a python built-in function, Returns whether an object is an instance of a class or its child class. If true, returns false instead.
2, Iterators: An object that can remember where to traverse
Iterator contains: File handle
2.1, the value of the iterator
#取值方式 use __next__ () S1 = ' ABCD ' s3 = s1.__iter__ () print (s3.__next__ ()) print (s3.__next__ ()) print (s3.__next__ ()) Print (S3 . __next__ ()) #输出结果A B C D
2.2, the meaning of iterators:
1), iterators save memory.2), the iterator inertia mechanism.3), iterators can not be repeated, always down execution.
2.3, for loop mechanism:
1), internal contains the __iter__ method, which will iterate the object first into an iterator, and then call the __next__ method.2), it has a method of exception handling.
The #实例1 for loop perfectly implements the iteration for I in [end=]: print (I, M, ") #1 2 3# instance 2 while loop requires exception handling statement to implement iteration L = [1,2,3]l_iter = l.__i TER__ () while True: try: print (l_iter.__next__ (), end= ") #1 2 3 except stopiteration: break
Second, generator
1.1. Definition: The function using yield is called the generator (generator), which is essentially an iterator, which is an iterator written in Python code.
1.2. Characteristics:1), you can use the generator function.2), you can use a variety of derivation to build an iterator.3), can be converted by data.
#普通函数def func (): return 222ret = func () print (ret) #111 # Generator Function--generator def gener (): yield 111 yield 222g = Gener () print (g) #<generator object Gener at 0x000001c8e10a43b8>print (g.__next__ ()) #111print (g.__next__ () ) #222 the difference between #return and yield #return returns to the caller value and ends the function. #yiled returns to the caller value and pauses the pointer over the current position.
1.3. Send function1), send and Next functions2), send a value to the previous yiled as a wholeNote: Send cannot send a value to the first and last yield, only with next
Def gener (): yield 111 count = Yield 222 print ('---', count) yield 333 yield 444g = Gener () Print (g.__next__ ()) print (G.send (' AAA ')) print (G.send (' BBB ')) #count--Bbbprint (G.send (' Last ')) #输出结果111222-BBB333444
Python iterators and generators