[Q] is 'yield from' syntax sugar for 'for'+'yield'?

Chris Angelico rosuav at gmail.com
Thu Aug 14 06:02:46 EDT 2014


On Thu, Aug 14, 2014 at 7:59 PM, Makoto Kuwata <kwatch at gmail.com> wrote:
> I understand that 'val = yield from xs' is completely different from::
>
>    for x in xs:
>       ret = yield x
>    val = x
>
> Return value is propagated by StopIteration, like:
>
>    it = iter(xs)
>    try:
>      while 1:
>        yield next(it)
>    except StopIteration as ex:
>      val = ex.value

It's even more complicated than that. The PEP specifies the exact semantics.

ChrisA



More information about the Python-list mailing list