1, iterator protocol:
1.1 Iterator protocol means that an object must provide a next method that either returns the next item in the iteration, or causes a Stopiteration exception to terminate the iteration (only backward cannot go forward)
1.2. An iterative object: An object that implements an iterator protocol (how to: Define an __iter__ () method within an object)
1.3. The Protocol is a convention that iterates over objects, implements an iterator protocol, and Python's internal tools (such as for loops, sum,min,max functions, etc.) use an iterator protocol to access an object
2, Iterator:
1.1: Why iterators are used:
1.2 Advantages: Iterators provide a way to not rely on the index, so that you can traverse those non-indexed iterative objects, such as: (Dictionary, collection, file)
Iterators are compared in lists, and iterators are lazy calculations, which save memory.
1.3 Cons: Unable to get the length of the iterator, the list operation is not as flexible. A one-time value can only be taken from the front to the back, not from the forward value.
1.4 Iterative, as long as the object itself has the __iter__ method, the resulting is an iterator.
1.5 lists list, tuple tuple, dictionary dict, set set, String str, file files, all can be iterated.
1.6 Example: Stupid method
D = {"a": 1,"b": 2,"C": 3 = d.__iter__() # turns the dictionary into an iterative object, I is the iterator print(I.__next__ ())print (i.__next__())print(i.__next__ ())
1.7 Operation Result: The K in the dictionary is printed out.
1.8 Use while to traverse the dictionary:
D = {"a": 1,"b": 2,"C": 3 = d.__iter__() while True: Print(I.__next__ ())
1.9 The running result dictionary can be all printed out, but while is a dead loop, traversing the three k in the dictionary, throws the stopiteration exception, we will handle it.
Atraceback (most recent): BC " f:/pycharmprojects/py_fullstack_s4/day23/iterators. PY " in <module> print(i.__next__()) Stopiteration
2.0 change the code to this:
D = {"a": 1,"b": 2,"C": 3 = d.__iter__() while True: try: Print(I.__next__()) except stopiteration: break
2.1 Try, except is the statement that removes the exception, the result of the operation is as follows:
Abc
2.2 Traverse for A For loop:
D = {"a": 1,"b": 2,"C": 3,} for in D: print(k)
2.3 Because for is very good, he only then turn D into an iterator, run the result is, A,b,c,
2.4 Viewing the object and iterator objects that can be iterated:
s ="Hello World"#字符串 strL= ["h","e","L","L""o"] #列表 Listt= ("W","o","R","L","D") #元组 tubled= {"name":"Gaoyuan","Job":"IT"," Age": 25} #字典 Dictset1= {1,2,3,4,5,} #集合 setf= Open ("a.txt") #文件 file#Are iterative: There are __iter__ methods.#s.__iter__ ()#l.__iter__ ()#t.__iter__ ()#d.__iter__ ()#set1.__iter__ ()#f.__iter__ ()Print(Isinstance (s,iterable))Print(Isinstance (l,iterable))Print(Isinstance (t,iterable))Print(Isinstance (d,iterable))Print(Isinstance (set1,iterable))Print(Isinstance (f,iterable))#The returned result is true#See if it is an iterator, only the file has the __next__ method,F.__next__()Print(Isinstance (S,iterator))#FalsePrint(Isinstance (L,iterator))#FalsePrint(Isinstance (T,iterator))#FalsePrint(Isinstance (D,iterator))#FalsePrint(Isinstance (Set1,iterator))#FalsePrint(Isinstance (F,iterator))#True#only files are iterators
3, Generator:
3.1 What is a generator?
Can be understood as a data type that automatically implements the iterator protocol (other data types need to call their own built-in __iter__ method), so the generator is an iterative object
Generator classification and representation in Python: (Python provides generators in two different ways)
A generator is a function in which the yield keyword is the generator
The difference between return and yield
Return returns once the value is completely over, yield can return multiple values
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, it resumes 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
3.2 Advantages of using the Generator builder
3.3 Python uses the generator to provide support for deferred operations. The so-called deferred operation refers to producing results when needed, rather than immediately producing results. This is also the main benefit of the generator.
3.4 Generator Summary:
3.4.1. is an iterative object
3.4.2. Delay calculation, save memory Ah
3.4.3. The generator is essentially the same as the other data types, all of which implement the iterator protocol, except that the generator attaches a delay to compute the benefits of saving memory, and the rest of the iterative objects do not have this benefit, remember!!!
。
Python base-------iterators, generators, and process functions