On Mon, Nov 13, 2006 at 01:44:10PM -0500, George Sakkis wrote:
> As a proof of concept, I provide below a sample implementation of how
> I imagine this type to work (also posted in the original c.l.py.
> thread):
>
> from itertools import chain, tee, islice
>
> import __builtin__
> _builtin_iter = __builtin__.iter
>
> class iter(object):
>
> def __init__(self, iterable):
> self._it = _builtin_iter(iterable)
>
> def __iter__(self):
> return self
> def next(self):
> return self._it.next()
>
> def __getitem__(self, index):
> if isinstance(index, int):
> try: return islice(self._it, index, index+1).next()
> except StopIteration:
> raise IndexError('Index %d out of range' % index)
> else:
> start,stop,step = index.start, index.stop, index.step
> if start is None: start = 0
> if step is None: step = 1
> return islice(self._it, start, stop, step)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. 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). > 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. 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. 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. ~harring
pgpGfJS8mNvn5.pgp
Description: PGP signature
_______________________________________________ Python-3000 mailing list [email protected] http://mail.python.org/mailman/listinfo/python-3000 Unsubscribe: http://mail.python.org/mailman/options/python-3000/archive%40mail-archive.com
