Python 3 iterator and Python 3 Generator

Source: Internet
Author: User

Python 3 iterator and Python 3 Generator

1 ''' 2 generators are all iterators. The iterator is not necessarily generator 3''' 4 5 # list1 = [1, 2, 3, 4, 5] 6 # p1 = iter (list1) # equivalent to _ iter _ () 7 # print (p1) 8 # print (next (p1) 9 # print (next (p1 )) 10 # print (next (p1) 11 # print (next (p1) 12 13 14''' 15 The iterator must meet the following two conditions: 16 1. there is iter Method 17 2. there are three things in the next Method 18 '''19 20''' 21 for loop: 22 1. call the iter method of the iteratable object and return an iterator object 23 2. call the next method of the iterator object 24 3. process StopIteration25 ''' 26 27 # for I in [5, 6, 7]: 28 # iter ([5, 6, 7]) 29 30 # from collections import Iterator, Iterable # Iterator, iteration object 31 # print (isinstance ([3, 4], list) # determine whether the previous parameter is the type32 # print (isinstance (6, list) written by the following parameter )) # True; otherwise, False33 #34 #35 # list2 = [,] 36 # p2 = iter (list2) 37 # print (p2) 38 # print (isinstance (list2, list) # whether it is list 39 # print (isinstance (list2, Iterable) # whether it is an iteration object 40 # print (isinstance (list2, Iterator )) # Whether it is an Iterator 41 # print (isinstance (p2, Iterator) # whether it is an Iterator

 

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.