6.9 iterator
So far, you may have noticed that many container objects can use the for statement for loop:
This access style is clear, concise, and convenient. The application of the iterator is python traversal. In this scenario, the for statement calls the iter () method of the container object. The function returns an iterator object. The iterator object defines the method _ next _ () that can only access one element at a time _(). When there are no elements in the container, this method will throw? Http://www.bkjia.com/kf/ware/vc/ "target =" _ blank "class =" keylink "> encrypt/decrypt + 3vbeoo7rS1M/C1eK49sD919PP1Mq + y/decrypt + IDxpbWcgc3JjPQ =" http://www.2cto.com/uploadfile/Collfiles/20140509/20140509091937247.jpg "alt =" \ ">
You must have seen the implementation mechanism behind the iterator. It is easy to add an iterator to your class. Define a _ iter _ () method to return an object containing the _ next _ () method. If the _ next _ () method is defined in the class, _ iter _ () will return itself.