On Mon, Nov 24, 2014 at 10:02 AM, Alexander Belopolsky < alexander.belopolsky@gmail.com> wrote:
On Mon, Nov 24, 2014 at 12:23 PM, Chris Angelico rosuav@gmail.com wrote:
Well, there is probably more to be said about this - along the lines of *why* generators ought to be more like iterators. (They're iterables, not iterators.)
If generators are not iterators, how do you explain this?
import collections def g():
... yield 42 ...
isinstance(g(), collections.Iterator)
True
I think Chris A was overzealous here. The word "generator" is ambiguous; it can refer to either a generator function (a function definition containing at least one "yield") or to the object you obtain by calling a generator function. The latter is definitely an iterator (it has a __next__ method). You can't really call a generator function an iterable (since calling iter() on it raises TypeError) but it's not an iterator either. For the rest see my explanation in response to Mark Shannon in python-dev: http://code.activestate.com/lists/python-dev/133428/