yield from () Was: Re: weirdness with list()

Chris Angelico rosuav at gmail.com
Tue Mar 2 16:51:59 EST 2021


On Wed, Mar 3, 2021 at 8:21 AM Dieter Maurer <dieter at handshake.de> wrote:
>
> Alan Gauld wrote at 2021-2-28 23:47 +0000:
> >yield from ()
>
> "yield from iterator" is similar to "for i in iterator: yield i" (with
> special handling when data/exceptions are injected into the generator).
>
> Thus, "yield from ()" does essentially nothing with the side effect
> that the containing function is treated as generator function.
>

Another way to write the same thing is:

if False: yield None

This, too, will do nothing - in fact, it will be optimized out
completely in current versions of CPython - but, again, will force the
function to be a generator.

ChrisA


More information about the Python-list mailing list