[Python-ideas] Generators are iterators

Chris Angelico rosuav at gmail.com
Wed Dec 10 23:22:27 CET 2014

On Thu, Dec 11, 2014 at 9:11 AM, Alexander Belopolsky
<alexander.belopolsky at gmail.com> wrote:
> I think the problem is with the term "generator function" because what we
> call "generator function" is neither a generator nor a function.  It is a
> callable that returns a generator.   To our excuse, this abuse of language
> is not uncommon.  People often say "integer function" when they mean a
> function with integer domain and range (or even just range).

Interesting. Are you saying the 'def' statement may produce a thing
that isn't a function, even though it calls itself a function?

>>> def gen():
...     yield 1
>>> type(gen)
<class 'function'>
>>> type(gen())
<class 'generator'>

It's a function that returns an iterator, same as this is:

>>> def nongen():
...     return iter((1,2,3))
>>> type(nongen)
<class 'function'>
>>> type(nongen())
<class 'tuple_iterator'>

A generator function is a function that returns a generator object. Is
this problematic?


More information about the Python-ideas mailing list