Proposed PEP: New style indexing, was Re: Bug in slice type

Bryan Olson fakeaddress at nowhere.org
Wed Aug 24 11:21:22 EDT 2005


Steven Bethard wrote:
 > Bryan Olson wrote:
 >
 >> Steven Bethard wrote:
 >>  > Well, I couldn't find where the general semantics of a negative 
stride
 >>  > index are defined, but for sequences at least[1]:
 >>  >
 >>  > "The slice of s from i to j with step k is defined as the sequence of
 >>  > items with index x = i + n*k such that 0 <= n < (j-i)/k."
 >>  >
 >>  > This seems to contradict list behavior though. [...]
 >>
 >> The conclusion is inescapable: Python's handling of negative
 >> subscripts is a wart.
 >
 >
 > I'm not sure I'd go that far.  Note that my confusion above was the
 > order of combination of points (3) and (5) on the page quoted above[1].
 >  I think the problem is not the subscript handling so much as the
 > documentation thereof.

Any bug can be pseudo-fixed by changing the documentation to
conform to the behavior. Here, the doc clearly went wrong by
expecting Python's behavior to follow from a few consistent
rules. The special-case handling of negative indexes looks
handy, but raises more difficulties than people realized.

I believe my PPEP avoids the proliferation of special cases. The
one additional issue I've discovered is that user-defined types
that are to support __getitem__ and/or __setitem__ *must* also
implement __len__.  Sensible sequence types already do, so I
don't think it's much of an issue.


 > This is already valid syntax, and is used
 > heavily by the numarray/numeric folks.

Yeah, I thought that variant might break some code. I didn't
know it would be that much. Forget that variant.


-- 
--Bryan



More information about the Python-list mailing list