2017/9/7 of learning content ——— Python iterator (old boy Alex)

Source: Internet
Author: User
Tags iterable

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 .

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 acting on for a loop, are called iterative objects:Iterable

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

 from Import iterable>>> isinstance ([],iterable) True>>> isinstance ('ABC  ','awe'), iterable) True>>> isinstance ({}, iterable) True>>> isinstance (123, 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.

  That is, we conclude that the object that can be called by Next () and returns the next value is: Iterator Iterator

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

Eg: list is not an iterator

  

>>> a = [a]>>>dir (a) ['__add__','__class__','__contains__', '__delattr__','__delitem__','__dir__'    , '__doc__','__eq__','__format__', '__ge__','__getattribute__', '__getitem__','__gt__','__hash__', '__iadd__','__imul__','__init__', '__iter__','__le__','__len__', '__lt__','__mul__','__ne__', '__new__','__reduce__', '__reduce_ex__','__repr__', '__reversed__','__rmul__', '__setattr__','__setitem__', '__sizeof__','__str__', '__subclasshook__','Append','Clear', 'Copy','Count','Extend','Index', 'Insert','Pop','Remove','Reverse', 'Sort']#There is no next call method in the list, not an iterator

There is no __next__ in the method that can be found in the list is not an iterator

  

 from Import Iterator  for  in range (5)), Iterator) True>>> list Generation (generator) is generator)>>> isinstance ({}, Iterator) False list, Ganso, etc. are not iterators

Turn from (King Horn)

List, dict, str and other iterable become iterator can use the built-in method: ITER ()

iter () function >>> a = [1,32,444]>>> b = iter (a)>>> B.__next__  ()1>>> B.__next__() 32>>> B.__next__()444> >>>>> B.__next__() Traceback (most recent):  "< stdin>" in <module>stopiteration throws exception when no data

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. We can think of this data stream as an ordered sequence, but we cannot know the length of the sequence in advance, but can only continue to calculate the next() next data on demand by the function, so Iterator The calculation is inert , It is calculated only if you need to return the next data.

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. (The advantages of iterators relative to lists)

 

2017/9/7 of learning content ——— Python iterator (old boy Alex)

Related Article

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.