[Python-3000] Builtin iterator type

George Sakkis gsakkis at rutgers.edu
Tue Nov 14 05:19:51 CET 2006


On 11/13/06, Brian Harring <ferringb at gmail.com> wrote:

> Tend to think __getitem__ on base iter is a rather bad idea due to the
> fact it implies the underlying iter is a mapping; it's not.

Only mappings define __getitem__ ?!? Now that's a new one.

> Additionally... islice *does* have issues when working against an
> iterator that hasn't been protected via tee; that's not a complaint
> against islice mind you, it's just reality that if you islice an
> iterator, make damn sure you pull from that islice before going
> back to the original iterator; intermixing them is a grand way to
> shoot yourself in the foot.
>
> Current usage, it's easy to spot it (look for islice); overloading
> getitem however means it gets far harder, thus more likely to trip
> folks up (in my opinion).

That might be a valid point, but according to this argument, Numpy
slices are evil too as they return a view and not a copy, like most
other sequences. There is a limit to polymorphism and duck typing; you
can't always pretend you don't care about the actual type.

> >     def __add__(self, other):
> >         return chain(self._it, other)
>
> The change to the iterator protocol here, that iter1 + iter2 ==
> chain(iter1, iter2) breaks down for self iterating types; trying to
> shoe horn in add/radd means that for self-iters, add/radd/mul are now
> taken away from them.

Excuse my ignorance, but what's a "self-iter" ?

> Duck typing works (imo) when the hook points all behave similiar; not
> convinced that abusing __add__ is wise, definitely not convinced
> __getitem__ is sane, especially since islice doesn't guarntee that
> it'll return that specific slice- just gurantees that it'll follow a
> specific pattern for consuming from the wrapped iter.

Care to provide an example of what you're talking about ?

> So... nuking those, you wind up with existing iter.  Perhaps providing
> sane examples of __getitem__ usage would help; add sort of makes sense
> sans it getting whacky for self-iters.

If by sane example you mean expectable, not necessarily useful, here's
a simple one off the top of my head:

>>> from itertools import count
>>> x = Iter(count(10))
>>> y = Iter('abcdefghij')
>>> list((x[:3] + y[7:]) * 2)
[10, 11, 12, 'h', 'i', 'j', 10, 11, 12, 'h', 'i', 'j']

Try rewriting it using explicit itertools calls and see what looks
more readable.

George


More information about the Python-3000 mailing list