generator/coroutine terminology
Marko Rauhamaa
marko at pacujo.net
Mon Mar 16 03:12:28 EDT 2015
Steven D'Aprano <steve+comp.lang.python at pearwood.info>:
> Marko Rauhamaa wrote:
>> What features do generator iterators provide on top of generic
>> iterators?
>
> The biggest difference is syntactic. Here's an iterator which returns a
> never-ending sequence of squared numbers 1, 4, 9, 16, ...
>
> class Squares:
> def __init__(self):
> self.i = 0
> def __next__(self):
> self.i += 1
> return self.i**2
> def __iter__(self):
> return self
>
>
> Here's the same thing written as a generator:
>
> def squares():
> i = 1
> while True:
> yield i**2
> i += 1
I was actually referring to the offered API. It still seems to me like
all iterators could offer close(), send() and throw(). Why? To make all
iterators drop-in replacements of each other.
Then, you could also stop wondering what to call the thingy returned by
a generator. Why, it would be an iterator. You wouldn't then have any
other use for a generator than the function that returns an iterator.
You could then decide if you want to reserve the name "generator" for
functions that contain a yield statement (syntactic notion) or call any
function returning an iterator a "generator" (semantic notion).
Marko
More information about the Python-list
mailing list