generator/coroutine terminology
Steven D'Aprano
steve+comp.lang.python at pearwood.info
Thu Mar 12 12:27:54 EDT 2015
Rustom Mody wrote:
> This is more a question about standard terminology/conventions than about
> semantics - of course assuming I understand :-)
>
> Say I have a simple yielding function:
>
> def foo(x):
> yield x+1
> yield x+2
>
> And I have
>
> g = foo(2)
>
> If I look at type, g's type is 'generator' whereas foo is just plain-ol
> 'function.'
>
> Whereas in informal usage we say foo is a generator.
Hopefully it is clear from context what we actually mean. When in doubt, we
should be explicit.
> So the question:
> What should we call foo and what should we call g?
foo is a generator function, i.e. a function which returns a generator. I'd
also allow generator factory, naming it by intent rather than type.
The result of calling foo, namely foo(x), which you have named g, is a
generator object, which is a kind of iterator.
> Same applies when foo is a 'coroutine' ie
> something having yield used in an rhs and used with '.send' from outside:
> What to call foo and what to call foo(x)?
foo is a generator function, and foo(x) is a generator object. In this case
we can call foo(x) by usage rather than type, and call it a coroutine.
--
Steven
More information about the Python-list
mailing list