+ 1
Generators
Hello. Can anyone please explain to me what are generators? I'm just having trouble understanding it.
2 Réponses
0
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.
0
Thank you.