Python3.2 official document translation-iterator

Source: Internet
Author: User

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.


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.