inside-out range function

William Clifford mr.william.clifford at gmail.com
Tue Apr 28 05:27:07 CEST 2009


For some reason I thought I needed this code, but it turns out I
don't, really.
I need something weirder. Anyway, maybe someone else could use this.

def enrag(start, stop=None, step=1):
    '''Yield a range of numbers from inside-out, evens on left.'''

    if stop is None:
        stop, start = start, 0

    lstep = step * -2
    rstep = step * 2
    lstop = start - step

    if stop % step > 0:
        lstart = stop - (stop % rstep)
    else:
        lstart = stop - (rstep - (stop % rstep))

    for left in xrange(lstart, lstop, lstep):
        yield left

    for right in xrange(step, stop, rstep):
        yield right

--
William Clifford



More information about the Python-list mailing list