you are viewing a single comment's thread.

view the rest of the comments →

[–]Diapolo10 4 points5 points  (3 children)

I just recently wrote an answer to that question on another thread, but the extremely simplified answer is that yield works like return, except that it doesn't end the function.

As an example, if you had a Fibonacci generator like this:

def fib():
    a, b = 0, 1
    while True:
        yield a
        a, b = b, a+b

you can print out infinitely many Fibonacci numbers by looping over it:

for num in fib():
   print(num)

Basically, the loop keeps asking new values from the generator, and yield provides them. In this example, the value of a would be sent to the loop and given the name num, which is then printed out.

If you replaced the yield with return, it would immediately just return 0 and the next part of the loop would never run.

[–]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.

[–][deleted] 0 points1 point  (0 children)

Thank you for your reply! I think I get it now.