"Python" iterator

Source: Internet
Author: User

The concept of iterators and generators has been confusing, summarizing:

Iterators:

1. The so-called iterator is the object with the __next__ () method;

The 2.__iter__ () method returns an Iterator object that must have a __next__ () method;

3. An object that implements the __iter__ () method is iterative, and an object that implements the __next__ () method is an iterator;

4. Built-in function iter () calls the __iter__ () method from an iterative object and returns an Iterator object;

5. The built-in function next () invokes the __next__ () method and advances forward;

The 6.for loop automatically calls the built-in function iter () to obtain an iterator object using the __next__ () method;

7. The file object itself is an iterator, the generator iterator is itself, and the generator automatically supports the iterative protocol;

8. Built-in objects such as lists are not iterators themselves because they support multiple open iterators and need to use the built-in function iter () to start the iterator;

9.__ITER__ () loops only once, and then uses the need to create a new iterator

10. If you want to achieve the effect of multiple iterators, __iter__ () simply defines the new state object for the iterator instead of returning the self

11. If the iterator object is passed to the built-in function iter (), then this function will return the iterator, and if a container class object is passed to the ITER () function, the ITER () function will return a new iterator object each time, which can be used to determine if the input value is the iterator object itself.

Reference: Python Learning Handbook

Python Cookbook

Effective Python

Basic Python Tutorials

"Python" iterator

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.