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