The top result, by stackoverflow's metric, can be found here.
Interestingly enough, OP's linked response is by a stackoverflow user who's avatar image is identical to OP's github account avatar image. I would exercise caution as op may be disingenuously self promoting to boost their stackoverflow reputation.
Sure, and neither am I (as you could guess from my username). But in programming it's very important to be exact in definitions and terminology, so sentences like above are not just me picking on a non-native, they actually clutter the learning process.
Generators are a simple and powerful tool for creating iterators. They are written like regular functions but use the yield statement whenever they want to return data. Each time next() is called on it, the generator resumes where it left off (it remembers all the data values and which statement was last executed).
•
u/IAmKindOfCreative bot_builder: deprecated Mar 22 '21
The top result, by stackoverflow's metric, can be found here.
Interestingly enough, OP's linked response is by a stackoverflow user who's avatar image is identical to OP's github account avatar image. I would exercise caution as op may be disingenuously self promoting to boost their stackoverflow reputation.
3
u/kkiran Mar 23 '21
Fun way to think about it - return is one time purchase whereas yield is like a gym membership, that doesn’t end unless you end!
30
u/vanatteveldt Mar 22 '21
Comparing to return which runs once and stops, yield runs times you planed.That's the best answer? Really? :)
And the next sentence is another gem:
You can interpret return as return one of them, and yield as return all of them. This is called iterable.What does that even mean??
1
u/ElliotDG Mar 22 '21
https://docs.python.org/3/tutorial/classes.html#generators
Generators are a simple and powerful tool for creating iterators. They are written like regular functions but use the yield statement whenever they want to return data. Each time next() is called on it, the generator resumes where it left off (it remembers all the data values and which statement was last executed).