Generators | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1


Hello. Can anyone please explain to me what are generators? I'm just having trouble understanding it.

5th Apr 2021, 7:22 PM
Ailana - avatar
2 Answers
Sometimes it’s more efficient to generate only the data needed. Then each time you request it, it produces whatever value is next. This can be used to iterate through a file one line at a time, generate registration codes one at a time, etc. It’s a simple way to write a function that remembers where it was and where it is going after each yield. Imagine reading a text file that has a million lines in it, like a log file. Or producing sequencial product registration numbers whenever an item is sold. The generator will remember where it was and generate the next value. No need to loop or create global variables, etc.
5th Apr 2021, 8:35 PM
Jerry Hobby
Jerry Hobby - avatar
Thank you.
5th Apr 2021, 8:56 PM
Ailana - avatar