Iterators are a high-level concept in Python, an iterator is an object that implements an iterator protocol, and what is an iterator protocol?
The following two conditions are sufficient for a row. (1) The object implements the __iter__ () method, (2) The object implements the next () method, returns the current element, and points to the position of the next element, and throws a Stopiteration exception if the current position has no elements.
Look at the following example
650) this.width=650; "Src=" Http://s5.51cto.com/wyfs02/M02/88/60/wKioL1f0uGiATtRcAACLQZmHWiE997.png-wh_500x0-wm_3 -wmp_4-s_2024698996.png "title=" Selection _020.png "alt=" Wkiol1f0ugiattrcaaclqzmhwie997.png-wh_50 "/>
This description lists this object to implement the iterator protocol. If an object has a __iter__ () method, it is an iterative object. An iterator protocol can be used to traverse an iterative object, such as the following example:
#coding =utf-8a = Range (3) it = a.__iter__ () while 1:try:print It.next () except Stopiteration:break
Output Result:
1
2
In fact, the for statement is the syntax sugar that gets encapsulated by the container's iterator, the next () method of the call iterator, and the process of stopiteration processing, similar to the In/not in statement.
There are many advantages to iterators:
(1) Defines the interface of a unified access container, we can define our own iterators at any time, as long as the implementation of the iterator protocol is OK.
(2) The iterator has an inert evaluation feature, which can be evaluated when iterating to the current element, so it is well suited to traverse a very many container and save memory.
Look at the following example:
#coding =utf-8class fab (object): def __init__ (Self, max): self.max = max Self.n, self.a, self.b = 0, 0, 1 def __iter__ (self): return self def next (self): if self.n < self.max: r = self.b self.a, self.b = self.b, self.a + self.b self.n = self.n + 1 return r raiSe stopiteration () For one in fab (5): print one
The output results are as follows:
1
2
3
4
5
The memory used by the iterator will be smaller. Take a look at the following example:
Print sys.getsizeof (Fab (5)) Print sys.getsizeof ([1,2,3,4,5])
The output results are as follows:
650) this.width=650; "Src=" Http://s3.51cto.com/wyfs02/M02/88/64/wKiom1f0vn2jacRKAAAaMtP6CdQ048.png-wh_500x0-wm_3 -wmp_4-s_3947242905.png "title=" Selection _021.png "alt=" Wkiom1f0vn2jacrkaaaamtp6cdq048.png-wh_50 "/>
The same is to get the container containing the element 1,2,3,4,5, the iterator saves memory. There is another one, see the following example:
Print sys.getsizeof (FAB (5)) print sys.getsizeof (fab) print sys.getsizeof (fab) print sys.getsizeof (FAB (5000))
The output results are as follows:
650) this.width=650; "Src=" Http://s1.51cto.com/wyfs02/M01/88/60/wKioL1f0v7iziCTIAAAZsyckEmc810.png-wh_500x0-wm_3 -wmp_4-s_3435320578.png "title=" Selection _022.png "alt=" Wkiol1f0v7izictiaaazsyckemc810.png-wh_50 "/>
The memory consumed is not increased by the increase in the number of elements, so use iterators or iterative protocols when writing code.
A ITER () function is built into Python to return an iterator object that accepts a parameter that implements the __iter__ () method (that is, an iterative object) or an iterator. For a container with the __iter__ () method, __iter__ () also returns an Iterator object.
Python has itertools modules, the functions are implemented using iterators, the efficiency is very high, there is time you can go to understand.
Python's iterator protocol