How about adding slice notation to iterators/generators?

Steven D'Aprano steve at
Sat Oct 17 10:12:08 CEST 2009

On Thu, 15 Oct 2009 18:29:51 -0700, Eloff wrote:

> I was just working with a generator for a tree that I wanted to skip the
> first result (root node.)
> And it occurs to me, why do we need to do:
> import sys
> from itertools import islice
> my_iter = islice(my_iter, 1, sys.maxint)
> When we could simply add slice operations to generators?
> for x in my_iter[1:]:
>     pass

Feel free to extend the iterator protocol to your own iterators, but it 
is a deliberately simple protocol. Many general iterators *can't* provide 
random access to slices, at least not without a horrible amount of work. 
Slicing support was dropped from xrange() years ago because it was 
surprisingly difficult to get all the odd corner cases correct. Now 
imagine trying to efficiently support this:

def digits_of_pi():
    """Yield the decimal digits of pi."""
    # left as an exercise for the reader


Or this:

def spider(url):
    """Follow hyperlinks from url, yielding the contents of each page."""
    # also left as an exercise for the reader


If you want to support slicing, go right ahead, but please, I beg you, 
don't force me to support it in my iterators!


More information about the Python-list mailing list