Big talk python----iterators

Source: Internet
Author: User
Tags iterable

Iterators

We already know that there are several types of data that can be directly applied to a for loop :

A class is a collection of data types, such as list,tuple,dict,set,str , and so on;

A class is generator, including generators and generator function with yield .

These objects that can directly act on a for loop are called an iterative object:iterable.

You can use isinstance () to determine whether an object is a Iterable object:

 from Import iterable>>> isinstance ([], iterable) True>>> isinstance ({}, iterable) True >>> isinstance ('abc', iterable) True for in Range (), iterable) True>>> isinstance (iterable) False

The iterator can not only be used for a for loop, but can also be called by the next () function and return the next value until the last throw Stopiteration error indicates that the next value cannot continue to be returned.

☆ objects that can be called by the next () function and constantly return the next value are called iterators:Iterator.

You can use isinstance () to determine whether an object is a iterator object:

 from Import Iterator  for  in range (), Iterator) True>>> isinstance ([], Iterator) False>> > isinstance ({}, Iterator) False>>> isinstance ('abc', Iterator) False

generators are iterator objects, but list, dict, and Str are iterable, but not iterator.

To change the list, dict, str, etc. iterable to iterator you can use the iter () function:

>>> Isinstance (ITER ([]), Iterator) True>>> isinstance (iter ('ABC')  ), Iterator) True

You might ask, why are data types such as list, dict, and str not Iterator?

This is because the Python Iterator object represents a data stream , and the Iterator object can be called by the next () function and continuously return to the next data until the Stopiteration error is thrown when there is no data. This data stream can be viewed as an ordered sequence, but we cannot know the length of the sequence in advance, but we will only continue to calculate the next data on demand through the next () function, so the calculation of iterator is inert and is only calculated when the next data needs to be returned.

Iterator can even represent an infinitely large stream of data, such as the whole natural number. Using list is never possible to store all natural numbers.

Summary

All objects that can be used for a for loop are iterable types ;

All objects that can be used for the next () function are iterator types , which represent a sequence of lazy computations;

Collection data types such as list, dict, str , and so on are iterable but not iterator, but a iterator object can be obtained through the ITER () function.

Python's for loop is essentially implemented by constantly invoking the next () function , for example:

 for inch [1, 2, 3, 4, 5]: Pass

is actually exactly equivalent to:

# first Get Iterator object:it = iter ([1, 2, 3, 4, 5])#  loop: while True:     Try :         # get the next value:        x = Next (it)    except  stopiteration:        #  Exit loop break when encountering stopiteration        

Big talk python----iterators

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.