classification
Title: slice.indices with negative step and default stop
Type: behavior Stage: needs patch
Components: Documentation Versions: Python 3.2, Python 3.3, Python 2.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: docs@python Nosy List: BreamoreBoy, João.Sebastião.de.Oliveira.Bueno, daniel.urban, docs@python, mark.dickinson
Priority: normal Keywords: easy

Created on 2011-04-13 20:50 by daniel.urban, last changed 2012-11-04 07:31 by mark.dickinson. This issue is now closed.

Messages (5)
msg133694 - (view) Author: Daniel Urban (daniel.urban) * (Python triager) Date: 2011-04-13 20:50
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]
>>>
msg133703 - (view) Author: João Sebastião de Oliveira Bueno (João.Sebastião.de.Oliveira.Bueno) Date: 2011-04-13 23:41
I don't see this as a bug. The indices returned in both cases are exactly what you need to feed to range, in order to get the correct indices for the provided slice parameters.

Perceive that if for 
s = slice(None, None, -2)

It would return anything different from
(9, -1, -2)
It would be impossible to properly generate the desired indices. (With a 0 instead of -1, we would be missing the last index).

When you pass these numbers with the "[" "]" notation for being used as indices, the negative index is interpreted as "len(sequence) - index". In the case of "-1" in your example it is the same as "9" not the same as "one before zero".

I recommend closing this as not a bug.
msg133744 - (view) Author: Daniel Urban (daniel.urban) * (Python triager) Date: 2011-04-14 15:37
I see. Thanks for the explanation. If indeed this is the expected behaviour (as it seems), then I suggest clarifying the documentation. I think it would be good to mention that the returned values are for range(), and not for creating another slice object.
msg174741 - (view) Author: Mark Lawrence (BreamoreBoy) * Date: 2012-11-04 02:30
I think this should be closed as slice.indices behaves exactly as I expect it to, and the returned values are for any sequence and not just range().
msg174761 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2012-11-04 07:31
Agreed.  Closing.
History
Date User Action Args
2012-11-04 07:31:33mark.dickinsonsetstatus: open -> closed
resolution: not a bug
messages: + msg174761
2012-11-04 02:30:31BreamoreBoysetnosy: + BreamoreBoy
messages: + msg174741
2011-11-19 14:08:36ezio.melottisetkeywords: + easy
stage: needs patch
versions: + Python 2.7, Python 3.3, - Python 3.1
2011-04-14 15:37:59daniel.urbansetnosy: + docs@python
messages: + msg133744

assignee: docs@python
components: + Documentation, - Interpreter Core
2011-04-13 23:41:06João.Sebastião.de.Oliveira.Buenosetnosy: + João.Sebastião.de.Oliveira.Bueno
messages: + msg133703
2011-04-13 20:50:52daniel.urbancreate