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__())
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.