[Python-Dev] sequence slice that wraps, bug or intention? (original) (raw)

Paul Moore p.f.moore at gmail.com
Fri Apr 3 12:29:17 CEST 2009


2009/4/3 Ulrich Eckhardt <eckhardt at satorlaser.com>:

Hi!

I just stumbled across something in Python 2.6 where I'm not sure if it is by design or a fault: x = 'abdc' x[-3:-3] -> '' x[-3:-2] -> 'b' x[-3:-1] -> 'bc' x[-3: 0] -> '' The one that actually bothers me here is the last one, I would have expected it to yield 'bcd' instead, because otherwise I don't see a way to specify a slice that starts with a negative index but still includes the last element. Similarly, I would expect x[-1,1] to yield 'ca' or at least raise an error, but not to return an empty string. Bug?

Feature. Documented behaviour, even (http://docs.python.org/reference/expressions.html#id5 section "Slicings").

This question is more appropriate for python-list (comp.lang.python) as it is about using Python, rather than the development of the Python interpreter itself (although I can see that your uncertainty as to whether this was a bug might have led you to think this was a more appropriate list). You should first confirm on python-list that a given behaviour is a bug, and if it is, post it to the tracker, rather than to python-dev.

In this case, the behaviour is fine. As regards your point "I don't see a way to specify a slice that starts with a negative index but still includes the last element" what you want is x[-3:].

If you want to discuss this further, please do so on python-list.

Paul.



More information about the Python-Dev mailing list