slicings: 3 questions

Alan G Isaac alan.isaac at gmail.com
Thu Jan 29 22:33:41 CET 2009


> On Thu, Jan 29, 2009 at 10:01 AM, Alan G Isaac <alan.isaac at gmail.com> wrote:
>> 2. It seems that slice objects and range objects are
>> awfully similar in many ways.  Is this "appearance only",
>> or was there any discussion of unifying them?
>> Curious for insight...


On 1/29/2009 1:37 PM Chris Rebert apparently wrote:
> Wouldn't really be possible, IMHO. True, they both have notions of
> start, stop, and step, but slices don't make sense as ranges without
> knowing the length of the container.

Slices seem somewhat more general than ranges.


> For example, take the slice
> `1:-2:1`. This is somewhat equivalent to range(1, -2, 1). However,
> since  -2 < 1 (stop < start ) and 1 (the step) is positive, the range
> is nonsensical.

Or rather, it makes sense, but is empty.
But I take your point.

However, I would turn it around slightly and ask:
when is it not the case that
range(*slice(start,stop,step).indices(stop)) != range(stop,start,step)

If there are no interesting cases, then it seems
that range might derive from slice.
Just curious...


> You have to replace all the negative indices with calculated positive
> indices first in order to have a sensical range(), in this case,
> replacing the stop of -2 with len(container)-2.
> Also, more fundamentally, Python is liberal in what it allows for the
> parts of slices, so unifying slices with ranges would break code. For
> example, Python is perfectly happy if I go slice("a",[8],object), none
> of which are even numbers.

Ah, this is new in Python 3 I take it?
I was not aware of this.  Use case?

Thanks!
Alan



More information about the Python-list mailing list