We can use lists to store data, but when the data is large, creating a list of stored data will take up memory. The generator is a method that does not take up much computer resources.
After the Python generator function is defined, it must be assigned to a variable during use. This is to generate an object for the generator. Each time the generator function is executed, a new object is created.
def fun():
print("I am the first paragraph")
yield 123
print("I am Zhang San")
yield 456
print("I am Li Si")
yield 789
print("Liu Wei is the last paragraph")
yield 000
print(fun().__next__()) # These three paragraphs will be an effect, because each time the fun() function is executed, a new generator object will be created
print(fun().__next__()) #So these three prints are three different generators
print(fun().__next__())
g=fun() #Generate the generator here, the next next points to this generator, so it can always take the next one
print(g.__next__())
print(g.__next__())
print(g.__next__())
print(g.__next__())