Sequence iterators with __index__

schickb schickb at
Wed Jun 25 01:19:23 CEST 2008

On Jun 24, 3:45 pm, Matimus <mccre... at> wrote:
> > I think it would be useful if iterators on sequences had the __index__
> > method so that they could be used to slice sequences. I was writing a
> > class and wanted to return a list iterator to callers.  I then wanted
> > to let callers slice from an iterator's position, but that isn't
> > supported without creating a custom iterator class.
> Could you post an example of what you are talking about? I'm not
> getting it.

Interactive mock-up:

>>> a = ['x','y','z']
>>> it = iter(a)
>>> a[it:]
['x', 'y', 'z']
>>> a[it:]
['y', 'z']
>>> a[:it]
>>> a[it:]

This lets you use sequence iterators more general position indicators.
Currently if you want to track a position and slice from a tracked
position you must do it manually with an integer index. It's not
difficult, but given that sequence iterators already do that already
it seems redundant (and of course more error prone).

> In any case, the first step is writing a PEP.

Ok thanks, but I do want some idea of interest level before spending a
bunch of time on this.


More information about the Python-list mailing list