Message132319
The problem still exists in current trunk:
The slicing semantics have been removed from the expressions reference:
http://docs.python.org/py3k/reference/expressions.html#slicings
The datamodel and types sections still have the same equations:
http://docs.python.org/py3k/reference/datamodel.html#types
http://docs.python.org/py3k/library/stdtypes.html#typesseq
Here's a synopsis of the problem, in code:
>>> # positive step, works as described
>>> i, j, k = 2, 8, 2
>>> range(10)[i:j:k]
[2, 4, 6]
>>> [i + n * k for n in range(0, (j - i) / k)]
[2, 4, 6]
>>> [range(10)[i] for i in _]
[2, 4, 6]
>>> # negative step, does not work as described
>>> i, j, k = 10, 0, -2
>>> range(10)[i:j:k]
[9, 7, 5, 3, 1]
>>> [i + n * k for n in range(0, (j - i) / k)]
[10, 8, 6, 4, 2]
>>> [range(10)[i] for i in _]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: list index out of range
>>> # actual behavior trims 10 to 9 (is the same as 9:0:-2)
>>> # that is, it trims to len(s) - 1, not len(s)
>>> range(10)[9:0:-2]
[9, 7, 5, 3, 1]
I propose to ignore the definition in the datamodel section, since it's talking about extended slicing in general, and doesn't mention anything about negative indices.
I propose the attached patch to fix the definition in the types section, which simply changes the statement:
If i or j is greater than len(s), use len(s)
to say:
If *i* or *j* is greater than len(s), use len(s) if *k* is positive,
or len(s) - 1 if *k* is negative. |
|
Date |
User |
Action |
Args |
2011-03-27 12:47:53 | bethard | set | recipients:
+ bethard, georg.brandl, logistix, docs@python |
2011-03-27 12:47:53 | bethard | set | messageid: <1301230073.1.0.37794455399.issue1446619@psf.upfronthosting.co.za> |
2011-03-27 12:47:52 | bethard | link | issue1446619 messages |
2011-03-27 12:47:52 | bethard | create | |
|