Kay Schluehr 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. Indexing from the high end is too useful >>to give up, but it should be specified by the slicing/indexing >>operation, not by the value of the index expression. > > > It is a Python gotcha, but the identity X[-1] == X[len(X)-1] holds and > is very usefull IMO.
No question index-from-the-far-end is useful, but I think special-casing some otherwise-out-of-bounds indexes is a mistake. Are there any cases in popular Python code where my proposal would not allow as elegant a solution? > If you want to slice to the bottom, take 0 as > bottom value. The docs have to be extended in this respect. I'm not sure what you mean. Slicing with a negative step and a stop value of zero will not reach the bottom (unless the sequence is empty). In general, Python uses inclusive beginning bounds and exclusive ending bounds. (The rule is frequently stated incorrectly as "inclusive lower bounds and exclusive upper bounds," which fails to consider negative increments.) -- --Bryan -- http://mail.python.org/mailman/listinfo/python-list