One, the iterator protocol definition:
Iteration: is a repeating process, each repetition, based on the last result
whileTrue:#simple repetition. Print('What are you talking about?') L=['a','b','C','D']count=0 whileCount <Len (l):Print(L[count]) Count+=1
An iterator protocol is an object that 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)
Two, can iterate the object definition:
An object that implements an iterator protocol (how to: Define an __iter__ () method within an object) is a __iter__ method under an object: an object. __iter__, which is an iterative object
s='Hello'L=['a','b','C','D']t=('a','b','C','D') DiC={'name':'Egon','Sex':'m'," Age": 18}set1={1,2,3}f=open ('Db.txt') s.__iter__() L.__iter__() T.__iter__() dic.__iter__() Set1.__iter__() F.__iter__() The above is an iterative object
Third, the Iterator object definition:
An iterative object executes the built-in __iter__ method, resulting in an iterator object
1, there is __iter__, the execution gets still the iteration itself
2, there are __next__
3, not dependent on the index of the value method
dic={'name':'Egon','Sex':'m'," Age": 18}i=dic.__iter__()#print (i) #iterator迭代器#i.__next__ () #next (i)Print(Next (i))Print(Next (i))Print(Next (i))Print(Next (i))#stopiterationL=['a','b','C','D']i=l.__iter__()Print(Next (i))Print(Next (i))Print(Next (i))Print(Next (i))Print(Next (i))#stopiteration
Iv. Advantages of Iterator objects
1: Provides a unified (index-independent) iterative approach
2: The iterator itself, which saves memory more than other data types
l=['a','b','C','D']i=ITER (l) DIC={'a': 1,'b': 2}x=Dic.keys ()Print(x) I=x.__iter__() with open ('a.txt') as F:#print (Next (f)) #print (Next (f)) #print (Next (f))F.read ()
V. Disadvantages of Iterator objects
1: One-time, can only go backward, can not be returned, the index value is flexible
2: Cannot predict when the value end, that is, cannot predict the length
l=['a','b','C',' D ' ]i=iter (l)print(Next (i))print(next (i )) Print(Next (i))
Six, for loop and iterative objects
Anything that can be used for loops is an iterative object
l=['a','b','C',' D ' ]for in#iter_l=l.__iter__ () print( Item) for in {1,2,3,4}: print(item)
Vii. judging an iterative object and an iterator object
Requires from collections import Iterable,iterator
Use one of the isinstance () to determine whether an iterator object is an iterative object
fromCollectionsImportiterable,iterators='Hello'L=['a','b','C','D']t=('a','b','C','D') DiC={'name':'Egon','Sex':'m'," Age": 18}set1={1,2,3}f=open ('a.txt')#Judging an Iterative objectPrint(Isinstance (s,iterable))Print(Isinstance (l,iterable))Print(Isinstance (t,iterable))Print(Isinstance (dic,iterable))Print(Isinstance (set1,iterable))Print(Isinstance (f,iterable))#To determine the iterator objectPrint(Isinstance (s,iterator))Print(Isinstance (l,iterator))Print(Isinstance (t,iterator))Print(Isinstance (dic,iterator))Print(Isinstance (set1,iterator))Print(Isinstance (F,iterator))
Python iterator protocol and iterative object, iterator object