[New-bugs-announce] [issue11842] slice.indices with negative step and default stop

Daniel Urban report at bugs.python.org
Wed Apr 13 22:50:53 CEST 2011


New submission from Daniel Urban <urban.dani+py at gmail.com>:

slice.indices behaves strangely with negative step and default stop values (note that the doc says that it "computes information about the slice that the slice object would describe if applied to a sequence of length items"):

>>> s = slice(None, None, -2)
>>> s.indices(10)
(9, -1, -2)
>>> list(range(10))[9:-1:-2]
[]
>>> list(range(10))[s]
[9, 7, 5, 3, 1]
>>> 

Also with start given:

>>> s = slice(8, None, -2)
>>> s.indices(10)
(8, -1, -2)
>>> list(range(10))[8:-1:-2]
[]
>>> list(range(10))[s]
[8, 6, 4, 2, 0]
>>> 

Strangely giving these indices to range works:

>>> s = slice(8, None, -2)
>>> s.indices(10)
(8, -1, -2)
>>> list(range(8, -1, -2))
[8, 6, 4, 2, 0]
>>>

----------
components: Interpreter Core
messages: 133694
nosy: durban, mark.dickinson
priority: normal
severity: normal
status: open
title: slice.indices with negative step and default stop
type: behavior
versions: Python 3.1, Python 3.2

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue11842>
_______________________________________


More information about the New-bugs-announce mailing list