Generators versus Coroutines

Tim Peters tim.peters at gmail.com
Sun Aug 15 03:13:58 EDT 2004


[Tim Peters]
>> If you want a concept of yielding without delivering a value, that's
>> simply not a use case Python's generators intended to address.  If
>> you wish, you can adhere to a *convention* that "yield None"
>> (or "yield False", or "yield 42", ...) means "I'm not really delivering a
>> value".

[Timothy Fitz]
> It's not a use case, but why not?

In the years generators were discussed before they were implemented,
nobody asked for that.

> And should it be?

Not by my lights, no.

> I know generators are semi-coroutines, but the fact is that their
> useage outside of value generation is just as useful and should be
> adressed as such.  I do not see how accepting a plain yield would
> break anything at all.

The vast majority of generator applications (as you said
before,"generators are used almost exclusively for generation of lists
just-in-time", so you already know this) have no use for that; so, for
the vast majority of generator applications, allowing it anyway would
offer nothing of value, but would reduce the quality of compile-time
error-checking.

> I have read the PEP, twice, and I don't see why it -wasn't- addressed.

If someone asked for a thing, it got into the PEP.  Nobody wanted it.

> Seems to me to be a fairly large arbitrary decision.

For general coroutines it would have been.  For Simple Generators (the
PEP's title) I think it was the right decision -- "simple" isn't
consistent with piling on gimmicks.

If you want to change it, write a new PEP.



More information about the Python-list mailing list