[Python-ideas] Generators are iterators
chris.barker at noaa.gov
Sat Dec 13 01:40:14 CET 2014
On Fri, Dec 12, 2014 at 3:22 PM, Andrew Barnert <abarnert at yahoo.com> wrote:
> 1) It's not clear what is meant by an "iterator" -- when we use the term
> is seems like we are talking about a type, when we are really talking about
> "anything that conforms to the iterator protocol", so I'm going to
> introduce a new term: TIIP (Type Implementing the Iterator Protocol) --
> maybe that's too cute, but role with it for now.
> When you're writing actual code, you can test this with
> isinstance(collections.abc.Iterator). Of course ABCs don't _perfectly_
> cover their related protocols (in this case, if you create a type that has
> __iter__ and __next__ but __iter__ doesn't return self--or, of course, if
> you just register some random type with Iterator explicitly--you've created
> a subclass of Iterator that isn't a subtype of the iterator protocol). But
> I think it still makes sense to use Iterator as shorthand for "type that
> subclasses Iterator" with the implied "is an actual subtype of the semantic
> type intended by the Iterator class".
this issue here is that inteh PEP, and in this list, it seems "iterator"
was used to describe classes with __init__ and __next__ methods, AS APPOSED
to the things returned from functions with a yield in them...
In : g = (i for i in range(10))
In : isinstance(it, collections.abc.Iterator)
In : def gf():
....: yield 1
....: yield 2
In : gen = gf()
In : isinstance(gen, collections.abc.Iterator)
So generators ARE iterators, but his definition and yet the contrast was
> This kind of shorthand is used regularly in discussing types in every OO
> language, not just Python.
sure, but Python is dynamically types, so "conforms to a protocol" not at
all teh same is "isinstance", even though ABCS have made this closer...
> In Python, the term "Iterator" is just as consistent and meaningful as in
> all these other languages. The fact that some people confuse iterables and
> iterators isn't a reason to abandon this simplicity.
actually, the confusion between iterables and iterators want the issue I as
trying to address -- but the confusion with a sentence like
"Under this proposal, generators and iterators would be distinct, but related,
That has been removed from the PEP -- I think it's more clean now in any
But yes, I could have written all that and stuck with plain old "iterator"
instead of making up "TIIP"
Especially since it doesn't even help to solve the problem--as we've just
> seen in this very message I'm replying to, it's just as easy for someone to
> mistakenly use TIIP to describe "range" as it is for them to mistakenly use
My bad -- typing too fast! And this was never about the itterable vs
iterator distinction anyway.
> (In fact, it's arguably worse, because what would you call an Iterable
> under the same naming scheme but Type Implementing Iterable Protocol, or
I was not trying to mingle iterable and iterator -- really I wan't :-)
> The things we need to be clear about here are the things that _dont't_
> have an official name. In particular, the thing that's built from calling a
> generator function or evaluating a generator expression and used by the
now I am confused:
In : g = ( i for i in range(10) )
In : type(g)
how is that not a "generator"?
Christopher Barker, Ph.D.
Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception
Chris.Barker at noaa.gov
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Python-ideas