"26" Python iterator notes

Source: Internet
Author: User
Tags generator generator iterable throw exception

before you tell what an iterator is, one thing to differentiate: iterable is the difference between iterative and iterator iterators.

There are several kinds of data data types that can be directly scoped for loops:
A class is a collection of data types, such as list, Dict, str, set, tuple, etc.
A class of generator, including generators and generator function with yield
These are objects that can be directly acting on a for loop called an iterative object: iterable
You can use Isinstance () to determine whether an object is an Iterable object

from collections import Iterableprint(isinstance([],Iterable))  ##list列表 #Trueprint(isinstance({},Iterable))  ##dict字典 #Trueprint(isinstance("abc",Iterable)) ##str字符串 #Trueprint(isinstance(1,Iterable)) ##num数字 Falseprint(isinstance((x for x in range(10)),Iterable)) ##加上for循环 True

Returns true to indicate that the Iterable object is a.
The generator generator not only can be scoped for loop, but also can be repeatedly called by the next function and return the next value, knowing that the last throw exception stopiteration error indicates that the next value cannot continue to be returned.

An iterator that can be used for a loop and can also continue to return the next value with Next: Iterator
You can use Isinstance to determine whether an object is an iterator object

from collections import Iteratorprint(isinstance([],Iterator))  ##list列表 #Falseprint(isinstance({},Iterator))  ##dict字典 #Falseprint(isinstance("abc",Iterator)) ##str字符串 #Falseprint(isinstance(1,Iterator)) ##num数字 Falseprint(isinstance((x for x in range(10)),Iterator)) ##加上for循环 True

Generator generator are iterator objects, but list, dict, and Str are iterable, but they are not iterator.
The ITER function can be used to change list, dict, and str to iterable.

from collections import Iteratorprint(isinstance(iter([]),Iterator))  ##list列表 Trueprint(isinstance(iter({}),Iterator))  ##dict字典 Trueprint(isinstance(iter("abc"),Iterator)) ##str字符串 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.


Summarize:
1. Anything that can be scoped for loop is iterable
2. All function objects that can be next are iterator types, which represent a sequence of lazy computations.
3. Collection data Types list, str, dict, and so on are iterable but not iterator, but a iterator object can be obtained through the ITER function.

"26" Python iterator notes

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.