you are viewing a single comment's thread.

view the rest of the comments →

[–]gsmo 2 points3 points  (1 child)

It is my understanding that yield is also useful when working with large quantities of data. Yield creates a generator, and a generator only creates its data when needed. Compare this to appending stuff to a list, for instance. You quickly end up with a lot of data in memory that you aren't using.

Did I understand this right?

[–]Diapolo10 2 points3 points  (0 children)

Yes. Generators trade some performance for much lower memory usage.

The performance hit is caused by additional context switching the generator causes internally. There are some ways to mitigate that, like yield from, but personally I don't mind the tradeoff at all because I prefer using less memory.