How about adding slice notation to iterators/generators?

Carl Banks pavlovevidence at gmail.com
Sat Oct 17 02:28:25 CEST 2009


On Oct 16, 2:02 am, Bearophile <bearophileH... at lycos.com> wrote:
> Terry Reedy:
>
> > 1. islice works with any iterator; generator method would only work with
> > generators
>
> A slice syntax that's syntactic sugar for islice(some_iter,1,None) may
> be added to all iterators.

All custom iterators would then be burdened to support it.  (That is
more than enough reason for me to oppose it.)


> >2. iterator protocol is intentionally simple.<
>
> Slice syntax is already available for lists, tuples, strings, arrays,
> numpy, etc, so adding it to iterators too doesn't look like adding
> that large amount of information to the mind of the programmer.

Yes it would be, it'd be an unacceptable burden.

The difference between iterators and containers is that it's a lot
more common to write custom iterators, and a lot more useful to.  The
iterator protocol must be kept simple so that customizing is kept
simple, the more burdens you place on iterator implementors, the less
people will chose to use them, and the less useful iterators will be.

For objects that are not commonly used directly by the programmer,
adding a slice syntax to the protocol is absolutely ludicrous.
There's no justification for it.

Use islice(), or slice the iterable beforehand.  Leave it out of
iterators.


Carl Banks



More information about the Python-list mailing list