Problem with Python xrange

Peter Otten __peter__ at
Tue Jun 8 17:56:21 CEST 2004

Konstantin Veretennicov wrote:

> Write a class implementing the behaviour you need. You'll want to
> implement xrange interface and slicing protocol. It's not possible to
> subclass xrange (yet?), so you'll have to delegate.
class XRangeFactory(object):
    def __getitem__(self, index):
        if isinstance( index, slice):
            if index.step is None:
                return xrange(index.start, index.stop)
            return xrange(index.start, index.stop, index.step)
        return xrange(index)

makeRange = XRangeFactory()
assert list(makeRange[5]) == range(5)
assert list(makeRange[7:11]) == range(7, 11)
assert list(makeRange[7:19:2]) == range(7, 19, 2)


More information about the Python-list mailing list