[Python-Dev] Xrange and Slices

Oren Tirosh oren-py-d@hishome.net
Wed, 26 Jun 2002 09:27:18 -0400


On Wed, Jun 26, 2002 at 02:37:17AM -0400, Raymond Hettinger wrote:
> Wild idea of the day:
> Merge the code for xrange() into slice().
> So that old code will work, make the word 'xrange' a synonym for 'slice'

Nice idea.  Since xrange is the one more commonly used in everyday 
programming I'd say that slice should be an alias to xrange, not the other
way around.  The start, stop and step attributes to xrange would have to be
revived (what was the idea behind removing them in the first place?)

This would make it trivial to implement a __getitem__ that fully supports 
extended slice notation:

class Spam:
    def __getitem__(self, index):
        if isinstance(index, xrange):
            return [self[i] for i in index]
        else:
            ...handle integer index

Two strange things about xrange objects:
>>> xrange(1,100,2)
xrange(1, 101, 2)

It's been there since at least Python 2.0.  Hasn't anyone noticed this
bug before?

>>> dir(x)
[]
Shouldn't it have at least __class__, __repr__, etc and everything else
that object has?

	Oren