How about adding slice notation to iterators/generators?

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Sat Oct 17 04:12:08 EDT 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

digits_of_pi()[99000:12309988234:7901]

Or this:

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

spider()[999:154:-7]

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



-- 
Steven



More information about the Python-list mailing list