[Python-ideas] PEP 492 terminology - (native) coroutine objects
Oscar Benjamin
oscar.j.benjamin at gmail.com
Thu Apr 30 22:37:51 CEST 2015
Looking through PEP 492 I dislike the terminology.
With generators I can do:
>>> def f(): yield
...
>>> g = f()
>>> g
<generator object f at 0x7fb81dadc798>
So f is a generator function and g is generator. That's all pretty
clear and "generator" is IMO a great name. The term "generator
function" is also unambiguous for f.
With PEP 492 it seems that I would get something like:
>>> async def af(): pass
>>> ag = af()
>>> ag
<coroutine_object object af at 0x7fb81dadc828>
According to the terminology in the PEP af is a coroutine but since
the word coroutine also refers to some generators in a looser sense I
should use "native coroutine" to disambiguate. ag is a "coroutine
object" but again I should use "native coroutine object" to explicitly
name the type because without the word "native" it also refers to
other things.
I think that if the terminology is already ambiguous with related
language constructs before it's introduced then it would be better to
change it now. The word coroutine (without "native" modifier) is used
by the PEP to refer to both generator based coroutines and the new
async def functions. I think it's reasonable to use the word as a
generic term in this sense. Python's idea of coroutines (both types)
doesn't seem to match with the pre-existing general definitions but
they are at least generalisations of functions so it can be reasonable
to describe them that way loosely.
Greg's suggestion to call an async def function (af above) an "async
function" seems like a big improvement. It clearly labels the purpose:
a function for use in a asynchronous execution probably with the
asyncio module. It also matches directly with the syntax: a function
prefixed with the word "async". There would be no ambiguity between
which of af or ag is referred to by the term.
It seems harder to think of a good name for ag though. ag is a wrapper
around a suspended call stack with methods to resume the stack so
describing what is is doesn't lead to anything helpful. OTOH the
purpose of ag is often described as implementing a "minithread" so the
word "minithread" makes intuitive sense to me. That way async code is
done by writing async functions that return minithreads. An event loop
schedules the minthreads etc. (I'm trying to imagine explaining this
to someone...)
I'm not sure what the best word is for a coroutine object but the
current terminology clearly has room for improvement. For a start
using the word "object" in the name of a type is a bit rubbish in a
language where everything is an object. Worse the PEP is reusing words
that have already been used with different meanings so that it's
already ambiguous. A concrete builtin language type such as the
coroutine object deserves a good, short, one-word name that will not
be confused with other things.
--
Oscar
More information about the Python-ideas
mailing list