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

Makoto Kuwata kwatch at gmail.com
Thu Aug 14 05:59:25 EDT 2014


On Thu, Aug 14, 2014 at 6:38 PM, Chris Angelico <rosuav at gmail.com> wrote:

> On Thu, Aug 14, 2014 at 7:35 PM, Makoto Kuwata <kwatch at gmail.com> wrote:
> > I understand that::
> >
> >     yield from xs
> >
> > is syntax suger of::
> >
> >     for x in xs:
> >       yield x
>
> Not just. It's like that for simple cases, but there are edge cases
> that are much more complicated to do manually, and are simply taken
> care of. Best would be to read the PEP itself:
>
> http://www.python.org/dev/peps/pep-0380/
>
> ChrisA
>

Thank you. It seems too complicated...
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


Thanks.

--
regards,
kwatch
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20140814/d8010114/attachment.html>


More information about the Python-list mailing list