Python complaints
David Ascher
da at ski.org
Fri Nov 26 16:53:18 EST 1999
On Wed, 24 Nov 1999, Eric Jacobs wrote:
> I don't know if I like that syntax. Taking a slice
> of what? If I had to replace range, I'd go with:
>
> for i in Int[1:20:3]:
> ...
>
> Here Int is an object that represents a list of all
> integers. We take a slice of it... and get exactly
> what we expect.
> Either way, the semantics of lists with an infinite
> number of elements or negative indices would have
> to be cleared up somewhat. That would be a good
> thing.
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"
def __getslice__(self, start, stop):
return self.__getitem__(slice(start, stop, None))
Int = XRangeClass()
More information about the Python-list
mailing list