Python iterator and python Generator
1. yield: Convert the function into a generator (generator)
Example: Fibonacci Series
Def fib (num): a, B, c = 1, 0, 1 while a <= num: yield c B, c = c, B + c a + = 1for n in fib (10): print (n, end = '') #1 1 2 3 5 8 13 21 34 55
2. Iterable
All objects that can use the for loop, collectively referred to as Iterable)
From collections import Iterable, Iteratorprint (isinstance (fib (10), Iterable) print (isinstance (range (10), Iterable) # True
3. Iterator
You can use the next () <__ next _ ()> function to call an object that continuously returns the next value to become an Iterator (Iterator), indicating a sequence of inert computing.
List, dict, str is Iterable, not Iterator:
From collections import Iteratorprint (isinstance (list (), Iterator) # False
However, you can use the iter () function to change it to Iterator:
Print (isinstance (iter (list (), Iterator) # True