yield from

Chris Angelico rosuav at gmail.com
Tue Mar 24 19:11:53 EDT 2020


On Wed, Mar 25, 2020 at 10:08 AM Dan Stromberg <drsalists at gmail.com> wrote:
>
> Some time ago, when I first heard about yield from, it wasn't faster than a
> for loop yielding.
>
> Has that improved?

It's not about speed. It's about correctness, and the way it delegates
everything, not just "for x in iter: yield x". Here's what it's
actually equivalent to:

https://www.python.org/dev/peps/pep-0380/#formal-semantics

It'll be a LOT more efficient than writing all that out manually. For
the simple case where you really can just loop and yield, sure, though
IMO it's still clearer to yield-from than to loop-yield.

ChrisA


More information about the Python-list mailing list