Python builder, iterator, iterator object

Source: Internet
Author: User

Changing a list [] to () creates a generator: Generator,generator is the algorithm that is saved.

Data types that can be used for a For loop:
1. Collection type: List tuple dict set str
2, generator

An object that can directly act on a for loop is collectively known as: An Iterative object (iterable)

 from Import iterable Print (Isinstance ([],iterable)) Print (Isinstance ({},iterable)) Print  for  in range (iterable))print(isinstance (iterable))

Results:

Truetruetruefalse

An object that can be called by Next and continually returns the next value is called an iterator (Iterator)

 from Import Iterator Print (Isinstance ([],iterator)) Print (Isinstance ({},iterator)) Print  for  in range (Iterator))print(isinstance (Iterator))

Results:

 from Import Iterator Print (Isinstance ([],iterator)) Print (Isinstance ({},iterator)) Print  for  in range (Iterator))print(isinstance (Iterator))

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

#!/usr/bin/env python#-*-coding:utf-8-*- fromCollectionsImportIteratorPrint(Isinstance (iter([]), Iterator))Print(Isinstance (ITER ({}), Iterator))Print(Isinstance (x forXinchRange (10)) , Iterator))Print(Isinstance (ITER ('ABC'), Iterator))

Show Results:

Truetruetruetrue

Python builder, iterator, iterator object

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.