Talking about 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# 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
Summary
The above is all about the python iterator in this article. I hope it will help you. Interested friends can continue to refer to this site: python fun project-sharing of pornographic image recognition code, Python implementing a simple verification code program, Python algorithm output 1-9 arrays to form a result of 100 of all computational formulas, etc, if you have any questions, you can leave a message at any time. The editor will reply to you in a timely manner. Thank you for your support!