Given that we have this kind of arcane discussion fairly regularly (not just in this thread), and it always makes my head spin, and it seems I'm not the only one who gets confused: How about having a module that provides functions such as isgenerator isiterator isiterable etc. or alternatively one function that would return a tuple/list of categories that an object fell into e.g. ('iterator', 'iterable') or a dictionary e.g. { 'iterator' : True, 'iterable' : True, 'generator' : False }. (Bikeshed as appropriate, but providing a dict seems to make it easier to add more things in future without breaking backward compatibility.) Then those of us who are prepared to take care to be precise in our language but could do with some help could use it to clarify our thoughts. And there should be less noise in the newsgroups from pointless arguments about precisely what is what. And I suspect it would even have uses in "real" code. Rob Cliffe On 28/11/2017 06:22, Steven D'Aprano wrote:
On Tue, Nov 28, 2017 at 03:11:25PM +0900, Stephen J. Turnbull wrote:
Steven D'Aprano writes:
The subset of iterators which are created as generators are *also* called generators,
As long as we're being precise, I don't think that is precisely correct:
>>> (x for x in range(1))
>>> iter(range(1)) >>> iter((1,)) The two iterators have the same duck-type, the generator is different. How is the generator different? It quacks like a range_iterator and tuple_iterator, it swims like them, it flies like them. Is there some iterator method or protocol that generators don't support?
A generator (object) is, of course, an interable. And also an iterator:
py> collections.abc py> isinstance((x+1 for x in range(5)), collections.abc.Iterator) True
Most of the time the distinction doesn't actually matter, since you cannot (easily?) create a generator without first creating a generator function.
At least you can create a generator (object) with the generator function created and called implicitly by using a generator expression. Ah yes, I forget about generator expressions, thanks.