Ideas about how software should behave (was: replacing `else` with `then` in `for` and `try`)

Chris Angelico rosuav at gmail.com
Wed Nov 8 13:34:10 EST 2017


On Thu, Nov 9, 2017 at 5:20 AM, Ian Kelly <ian.g.kelly at gmail.com> wrote:
> On Wed, Nov 8, 2017 at 11:12 AM, Chris Angelico <rosuav at gmail.com> wrote:
>> Except that "yield from" is used by generators to delegate to other
>> generators, and "await" is used by coroutines to delegate to other
>> coroutines. In an asynchronous generator, "yield" produces values, and
>> "yield from" would delegate to another asynchronous generator. They
>> are NOT synonyms.
>
> Only because the devs have chosen to reserve the possibility of
> asynchronous generators. Abstractly, coroutines and generators are
> distinct concepts, but pragmatically, coroutines *are* generators.
> Native coroutines don't actually change this; they just do a better
> job of hiding it.

Coroutines *are implemented using* generators. And I don't know what
you mean by "reserve the possibility of"; asynchronous generators do
exist:

>>> async def gen():
...     yield 1
...     yield 2
...     yield 3
...     await something
...     yield 4
...
>>> gen()
<async_generator object gen at 0x7f9451ec51e0>

PEP 525 https://www.python.org/dev/peps/pep-0525/ says:
"""
While it is theoretically possible to implement yield from support for
asynchronous generators, it would require a serious redesign of the
generators implementation.
"""

In other words, it's only because of *implementation details* that
"yield from" inside a generator is difficult. There's no
language-level reason for it to be forbidden, and there is absolutely
NO correlation between "await" and "yield from" in an async function.

ChrisA



More information about the Python-list mailing list