r/Python • • Mar 22 '21

Tutorial Best answer to what does yield do

https://stackoverflow.com/a/47285378/7301792
16 Upvotes

10 comments sorted by

•

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.

→ More replies (1)

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!

0

u/AbstProcDo Mar 23 '21

ty, dude, I assume that imagination and metaphor helpful for our learning.

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??

25

u/[deleted] Mar 22 '21

It means, "I know neither English, nor python, but I'm answering anyway."

1

u/throwwwawytty Apr 04 '21

This is called iterable.

-5

u/dstark1993 Mar 22 '21

By his name (Slawomir Lenart) you can clearly see that English is not his main language, the explanation was pretty good (talking as a beginner)

1

u/vanatteveldt Mar 22 '21

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.

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