xslice idea | a generator slice

Russel Walker russ.pobox at gmail.com
Thu Jul 11 10:54:35 EDT 2013


...oh and here is the class I made for it.

class xslice(object):
    '''
    xslice(seq, start, stop, step) -> generator slice
    '''

    def __init__(self, seq, *stop):
        if len(stop) > 3:
            raise TypeError("xslice takes at most 4 arguments")
        elif len(stop) < 0:
            raise TypeError("xslice requires atleast 2 arguments")
        else:
            start, stop, step = (((0,) + stop[:2])[-2:] +  # start, stop
                                 (stop[2:] + (1,))[:1])    # step
            stop = min(stop, len(seq))
            self._ind = iter(xrange(start, stop, step))
            self._seq = seq

    def __iter__(self):
        return self

    def next(self):
        return self._seq[self._ind.next()]



Although now that I think about it, it probably should've just been a simple generator function.



More information about the Python-list mailing list