[ python-Bugs-1256786 ] slice object uses -1 as exclusive end-bound
SourceForge.net
noreply at sourceforge.net
Thu Aug 11 17:08:02 CEST 2005
Bugs item #1256786, was opened at 2005-08-11 08:08
Message generated for change (Tracker Item Submitted) made by Item Submitter
You can respond by visiting:
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1256786&group_id=5470
Please note that this message will contain a full copy of the comment thread,
including the initial issue submission, for this request,
not just the latest update.
Category: Python Interpreter Core
Group: Python 2.4
Status: Open
Resolution: None
Priority: 5
Submitted By: Bryan G. Olson (bryango)
Assigned to: Nobody/Anonymous (nobody)
Summary: slice object uses -1 as exclusive end-bound
Initial Comment:
The slice object passed to __getitem__ or __setitem__
reports an
incorrect 'stop' value when the step is negative and
the slice
includes the 0 index. If one then actually tries to
slice with
what slice.indices returns, the result is wrong. Here's
a demo:
class BuggerAll:
def __init__(self, somelist):
self.sequence = somelist[:]
def __getitem__(self, key):
if isinstance(key, slice):
start, stop, step =
key.indices(len(self.sequence))
# print 'Slice says start, stop, step
are:', start, stop, step
return self.sequence[start : stop : step]
print range(10) [None : None : -2]
print BuggerAll(range(10))[None : None : -2]
The above should print the same sequence twice, but
actually
prints:
[9, 7, 5, 3, 1]
[]
Un-commenting the print statement in __getitem__ shows:
Slice says start, stop, step are: 9 -1 -2
The problem is the stop value of -1. The slice object
seems to
think that -1 is a valid exclusive-end-bound, but when
slicing,
Python interprets negative numbers as an offset from
the high
end of the sequence. That is,
range(10)[9 : -1 : -2]
is the same as,
range(10)[[9 : 9 : -2]
which is the empty list.
So what should the slice.indices return in this case,
so that
slicing with the returned values will work correctly? My
experiments indicate:
The start value can be any of: None, any integer
>= 9, -1
The stop value can be either: None, any integer
<= -11
Step is correct; it must be: -2
My favorite choice here is (9, None, -2). The doc for
slice.indices currently says:
This method takes a single integer argument
/length/ and
computes information about the extended slice that
the slice
object would describe if applied to a sequence of
length
items. It returns a tuple of three integers;
respectively
these are the /start/ and /stop/ indices and the
/step/ or
stride length of the slice. Missing or
out-of-bounds indices
are handled in a manner consistent with regular slices.
http://docs.python.org/ref/types.html
So using (9, None, -2) would require changing both the
code and
the doc (because None is not an integer). A stop value
of -11
(or less) would require changing only the code.
----------------------------------------------------------------------
You can respond by visiting:
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1256786&group_id=5470
More information about the Python-bugs-list
mailing list