[Tutor] iterators
spir
denis.spir at gmail.com
Sun Jan 19 13:55:05 CET 2014
On 01/19/2014 12:24 AM, Keith Winston wrote:
> On Sat, Jan 18, 2014 at 2:19 PM, eryksun <eryksun at gmail.com> wrote:
>> `xrange` and 3.x `range` aren't iterators. They're sequences. A
>> sequence implements `__len__` and `__getitem__`, which can be used to
>> implement an iterator, reversed iterator, and the `in` operator (i.e.
>> `__contains__`).
>
> I'm so glad you said this, I'm sorta burned out right now trying to
> read all this, and I got sorta confused by that part. But what you're
> saying is what I thought I understood.
>
> Okay, now your example is pretty interesting. I guess it makes sense
> that iter() returns a type iterator. Sure, of course.
>
> Thanks as always to everyone, this is a trove. I'm a bit under the
> weather so I'll have to come back and read it closer. I'm a little
> clearer, though, and not just on iterators...
There is some inevitable confusion due to the exact usage or definition of given
terms in (the discourse) about given programming languages, as opposed to more
general meaings in programming in general (and to a certain point the meaning we
can infer from the ordinary sense of a term, when applied to programming).
Python for instance has a very precise usage and definition of "iterator" (as a
"protocal" for a kind of objects). This leads to some pythonists refusing or
correcting statements related to iterators which would otherwise be (mostly)
true in the more general context of programming (or which would be _differently_
wrong in the latter context ;-).
'range' ('xrange' in python2) is certainly (at least in my view) a kind of
iterator in the latter, more general sense used in programming (some thing
providing items one at a time); however, it does not implement python's iterator
protocal. Thus, it cannot be used directly in a 'for' traversal loop: if i'm
right, python builds a python iterator for ranges in the background. Like all
other kinds of 'sequences' (in the python sense, again) ranges are traversable
("iteratable") because they can in principle provide items one at a time, and
there exist builtin iterators for them.
For iterators, in python there is additional confusion with generators (at term
which AFAIK in programming means either about the same as iterator, or a
subclass of iterators implemented using poor man's coroutines), precisely
generator objects; and with generator expressions and other comprehensions.
A bit exaggerately complicated, in my view, esp when considering the narrowness
of the application field. Maybe a case of over-abstraction or over-engineering?
Denis
More information about the Tutor
mailing list