Python: iterator Iterator

Source: Internet
Author: User

Iterations are present in many languages, and in C + + you are more likely to use iterations rather than subscripts to access the data, let's look at the iterations in Python.

In Python we can use iterations to iterate through list,dirc, or even file, so let's first understand the iteration in the simplest way:

List = [+/-]
for I in list: #似乎和c + + for (auto Iter:vec) {} very much like dosomething

In C + + we can add and subtract iterator to move iterators, how is python implemented? Consult the Python documentation:

As you can see, Python has iterators,reverse iteration and copyable iterators

So let's take a look at iterators:

ITER () is an important function for implementing iterations, which is used to invoke the __iter__ () function in a class to obtain an Iterator.

For iterators, we can use for, or next () to work with it. Is there any other function? For example, get the element that is currently pointing? A previous element?

A: no, indeed, in the course of our actual use, almost no one uses next (), or any other function that gets the element, most of which is traversed by for. (the document even uses ' ugly ' to describe __next__ (), Python is really concise ~ ~)

Two uses of ITER (): ITER (obj), iter (callable, Sentinel)

Let's take a list to try, ITER () to the list to listiterator, also can use for to traverse, use once, after using next to get the next element display Stopiteration (iterator has been to the End)

So how do we make the class we define to implement iterator, so that he can also use for to traverse it?

It says in the documentation:

#Classes can define how they is iteratedover by defining an__iter__ and Return a valid iterator Object.  class  and__iter__() method that is returns SELF. # ##

To define the class implementation iterations need to define __next__ () and __iter__ () two methods, __iter__ () is used to return a special iterator object, and __next__ () is used to complete the actual Iteration.

finally, consider the performance of the Iterator:

It appears that iterators perform better than subscript iterations.

Python: iterator 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.