Python Learning-day6-iterator (iterator)

Source: Internet
Author: User
Tags iterable

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

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

One is generator to include the generator and yield the generator function with the band.

These objects, which can be directly applied to for the loop, are called iterative objects: Iterable .

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

 fromCollectionsImportiterableisinstance ([],iterable) trueisinstance ('ABC haha', iterable) Trueisinstance (Set ('Hello'), iterable) trueisinstance ({1:'a', 2:'b'},iterable) trueisinstance ((I* * forIinchRange (10) , iterable) trueisinstance (range (10), iterable) trueisinstance (1234, iterable) False

The generator can not only be used for for loops, but it can also be next() called by the function and return the next value until the last throw StopIteration error indicates that the next value cannot continue to be returned.

* An object that can be called by next() a function and continually returns the next value is Iterator called an iterator:.

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

Isinstance (Iterator)falseisinstance ({},iterator) falseisinstance ((I-in Range (+), Iterator) trueisinstance (range (ten), Iterator) False

Generators are Iterator objects, but,, list dict str Though Iterable they are, they are not Iterator .

Turn list , dict and str wait for the Iterable Iterator function to be used iter() :

Isinstance (ITER ([]), Iterator) Trueisinstance (ITER ((+)), Iterator) True

You may ask, why, list dict , str etc. data types are not Iterator ?

This is because the Python Iterator object represents a data stream, and the iterator object can be next() called by the function and will return the next data continuously until there is no data to throw an StopIteration error. You can think of this data stream as an ordered sequence, but we can't know the length of the sequence in advance, only by continuously using the next() function to calculate the next data on demand, so Iterator the calculation is lazy, and it will only be calculated when the next data needs to be returned.

IteratorIt 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

Any object that can be used for for the loop is a Iterable type;

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

Collection data types such as list , dict ,, and str so on are Iterable not Iterator , however, you can iter() get an object from a function Iterator .

The Python for loop is essentially implemented by calling next() functions, such as:

forin[12345]:

     passEquivalent:
# 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        

The above reference Alex Teacher's, the current iterator does not need to understand this concept first

Http://www.cnblogs.com/alex3714/articles/5765046.html

Python Learning-day6-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.