6.9iterators
Until now. You may have noticed that many container objects are able to Loop with a For statement:
This kind of interview style is clear and concise and convenient.
The application of iterators is python traversal unification. Behind such a scene. The FOR statement invokes the container object of the iter () method.
The function returns an Iterator object. The iterator object defines a method _next_ ()that can access only one element at a time. When there are no elements inside the container, the method throws a stopinteration exception to remind the For loop to terminate. You can call the _next_ method with the built-in next () : The following example shows how it works.
watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvc3himdg0mtkwmtexng==/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma== /dissolve/70/gravity/southeast ">
Presumably you have seen the implementation mechanism behind the iterators. Adding an iterator to your own class is very easy to implement. Defines a _iter_() method used to return an object that includes the _next_ () method. Assuming that the _next_ () method is defined in the class , then _iter_ () returns itself.
watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvc3himdg0mtkwmtexng==/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma== /dissolve/70/gravity/southeast ">
Python3.2 Official Document Translator--iterators