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

Ian Kelly ian.g.kelly at gmail.com
Wed Nov 8 13:20:01 EST 2017


On Wed, Nov 8, 2017 at 11:12 AM, Chris Angelico <rosuav at gmail.com> wrote:
> On Thu, Nov 9, 2017 at 5:05 AM, Ian Kelly <ian.g.kelly at gmail.com> wrote:
>> On Wed, Nov 8, 2017 at 9:31 AM, Chris Angelico <rosuav at gmail.com> wrote:
>>> On Thu, Nov 9, 2017 at 3:19 AM, Ian Kelly <ian.g.kelly at gmail.com> wrote:
>>>> I was not referring to the possible future use of yield from for async
>>>> generators; I was referring to the possibility *today* of using "yield
>>>> from" as a synonym for *await*. As far as I know the only major
>>>> obstacle to that is that the authors (with good reason) made it a
>>>> SyntaxError. This is exactly the same sort of situation: it's a
>>>> construct that would otherwise be perfectly valid, but it's made a
>>>> SyntaxError specifically to prevent users from doing some the devs
>>>> don't want them to.
>>>
>>> I don't understand why you would use "yield from" as a synonym for
>>> "await". They are not equivalent. Why would you use one in place of
>>> the other?
>>
>> There's not really a good reason to use "yield from" with "async def"
>> when you could just use "await", but the point is that in principle
>> you could. In a generator-based coroutine (e.g. asyncio prior to
>> Python 3.5), "yield from" is used to pause the coroutine and wait on
>> some future. In a native coroutine (e.g. after Python 3.5), "await" is
>> used to pause the coroutine and wait on some future. The
>> implementation AIUI is essentially the same; the __await__ method is
>> even required to return an iterator, just like __iter__.
>>
>> That's why I'm saying that they're basically synonyms. All that's
>> really separating them is the syntax error.
>
> 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.



More information about the Python-list mailing list