Python path-iterators

Source: Internet
Author: User

There are several types of data that can be directly used with a For loop: One is the collection data type: list, tuple, dict, set, str, etc. are generator, including generators and generator with yield Function These objects that can function directly with a For loop are called an iterative object: Iterable can use Isinstance () to determine whether an object is a Iterable object such as Isinstance ([],iterable) Iterators: Can be used for a For loop, can also be called by the __next__ () function and return the next value until the last throw stopiteration error means that the next value cannot be returned. An object that can be called by the __next__ () function and continuously returns the next value becomes an iterator: Iterator; You can use Isinstance () to determine whether an object is a Iterator object Iteration object (iterable) into an iterator (Iterator):
You can use the ITER () function to convert the list, dict, str, and other iterable into iterator
Note:
    1. The generator must be an iterator (iterator object), but list, dict, and Str are iterable objects but not iterator objects
    2. Import Iterable>>>from Collections Import iterable
    3. Import Iterator>>>from Collections Import Iterator
    4. The range in xrange and Python 3.x in Python 2.x is an iterator
Summarize:
    • All objects that can function with a for loop are iterable types
    • All objects that can function with the __next__ () function are iterator types, which represent a sequence of lazy computations
    • Collection data Type list equation iterable but not iterator, but can get a iterator object through the ITER () function

Python path-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.