Python3.2 Official Document Translator--iterators

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.