Python complaints
Eric Jacobs
x at x.x
Fri Nov 26 22:52:32 EST 1999
David Ascher wrote:
>
> Without dealing with the last paragraph, you can fake it now, with
> something like (barely tested):
>
> import types
> class XRangeClass:
> def __getitem__(self, item):
> if type(item) == types.SliceType:
> if item.start is None:
> start = 0
> else:
> start = item.start
> if item.step is None:
> step = 1
> else:
> step = item.step
> if item.stop is None:
> stop = sys.maxint
> else:
> stop = item.stop
> return xrange(start, stop, step)
> elif type(item) == types.IntType:
> return item
> else:
> raise ValueError, "XRanges needs to be sliced with indices or slices"
Actually, they can be sliced with tuples too (try Int[1:2, 4:5]). But you
get the idea. I don't think a change in Python syntax is needed to clean
up range and xrange.
Allowing slice indices to be any objects, and not just integers...
that could be a nice addition. Like in the Enumerator metaclass example:
you could have
for x in Color[red:blue]: ...
> def __getslice__(self, start, stop):
> return self.__getitem__(slice(start, stop, None))
>
> Int = XRangeClass()
More information about the Python-list
mailing list