MemotivaGenerators and yield

What is the difference between yield and return?

Generators and yield

Audio flashcard · 0:14

Nortren·

What is the difference between yield and return?

0:14

Return ends a function and sends a single value to the caller. Yield produces a value and suspends the function, preserving its state so it can resume later. A function with any yield becomes a generator function. Return inside a generator signals StopIteration with the returned value.
docs.python.org