generator/coroutine terminology

Chris Angelico rosuav at gmail.com
Mon Mar 16 04:58:22 EDT 2015


On Mon, Mar 16, 2015 at 7:36 PM, Steven D'Aprano
<steve+comp.lang.python at pearwood.info> wrote:
> I expect that the interpreter can tell the difference between
>
>     yield spam
>
> and
>
>     x = yield spam
>
> and only allow send(), close() and throw() on generators which include the
> second form. If it can't, then that's a limitation, not a feature to be
> emulated.
>

Hmm. That would imply that an expression is different if it's used
somewhere. In Python, there's no difference between these two function
calls:

x = func(1,2,3)
func(1,2,3)

except for the actual name binding. If a yield expression enabled
send() if and only if its return value were used, then it would be
extremely surprising:

def use_and_discard():
    while True:
        _ = (yield 1)

def just_yield():
    while True:
        yield 1

So... I can send into the first but not into the second?

That said, though, it would be quite reasonable for a *linter* to warn
you about sending into a generator that never uses sent values. With a
good type inference system (not sure if MyPy is sufficient here), it
would be possible to distinguish between those two functions and
declare that the first one has the return type "sendable_generator"
and the second one "nonsendable_generator", and give a warning if you
send into the latter. But I don't think that's the language's job.

ChrisA



More information about the Python-list mailing list