xslice idea | a generator slice

Russel Walker russ.pobox at gmail.com
Thu Jul 11 18:21:16 CEST 2013


> >     def __init__(self, seq, *stop):
> 
> 
> 
> Wouldn't it be better if it has the same signature(s) as itertools.islice?

That's actually what I was going for, except I was modeling it after range, but that was the only way I knew to implement it.


> >         if len(stop) > 3:
> 
> >             raise TypeError("xslice takes at most 4 arguments")
> 
> >         elif len(stop) < 0:
> 
> 
> 
> How would len(stop) be negative?


Yes, that should've been len(stop) < 1.


> Or you can use itertools.imap:
> 
> 
> 
> def xslice(sequence, start_or_stop, *args):
> 
>     indices = xrange(*slice(start_or_stop, *args).indices(len(sequence)))
> 
>     return imap(sequence.__getitem__, indices)
> 
> 
> 
> 
> 
> Oscar


I like the way you did that with imap, although I still like my parameter implementation.

To confess, this is the second time I've made the mistake of trying to implement generator like functionality of a builtin when there already is on in itertools. Need to start studying that module abit more I think. I'm looking at the docs now and I see there are actually a couple of isomethings().




More information about the Python-list mailing list