beginning index for generators

kosh kosh at
Sat Oct 16 14:56:27 CEST 2004

I was wondering if there is or there could be some way to pass a generator an 
optional starting index so that if it supported that slicing could be made 
more efficient. Right now if you do use a generator and do a [100:110] or any 
other kind of slice it reads all the values up to 100 also. 

I know a lot of generators have to do all previous parts before they can do 
the next part but it would be a nice optimization that can start at any index 
to take an optional argument to tell them what index they are currently 
working on. This could make strides more efficient also since it would not 
have to run the generator to skip the parts it was not using.

The reason I would like this to be part of the generator rather then making a 
special function to do this is that it would make it easier to refactor a 
piece of code to be a much more efficient generator from some other sequence 
type without having to change how it is called. 

I have no idea how feasible this is, what it would take to implement it etc 
but I did not get any matches searching this on google and wanted to throw it 
out here to see if others have run into situations in which this would be 
very useful also and would further simplify code and make it more readable.

I would also love to know of other ways that this problem could be solved but 
I would like it to be as transparent as possible to existing calling code.

More information about the Python-list mailing list