This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: Slice indexes passed to __getitem__ are wrapped
Type: behavior Stage:
Components: Interpreter Core Versions:
Status: closed Resolution: wont fix
Dependencies: 723806 Superseder:
Assigned To: Nosy List: ajaksu2, belopolsky, connelly, mwh
Priority: normal Keywords:

Created on 2004-06-17 10:22 by connelly, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (6)
msg60511 - (view) Author: Connelly (connelly) Date: 2004-06-17 10:22

When a slice is passed to __getitem__, the indices for
the slice are wrapped by the length of the object
(adding len(self) once to both start index if < 0 and
the end index if < 0).

class C:
  def __getitem__(self, item): print item
  def __len__(self): return 10

>>> x = C()
>>> x[-3]
>>> x[-3:-2]
slice(7, 8, None)

This seems to be incorrect (at least inconsistent).

However, it truly becomes a BUG when one tries to
emulate a list type:

class C:                      # Emulated list type
  def __init__(self, d): = d
  def __len__(self):
    return len(
  def __getitem__(self, item):
    if isinstance(item, slice):
      indices = item.indices(len(self))
      return [self[i] for i in range(*indices)]

x = [1, 2, 3]
y = C([1, 2, 3])

>>> x[-7:-5]
>>> print y[-7:-5]
[1]                         (incorrect behavior)

Here the item.indices method does the exact same
wrapping process AGAIN.  So indices are wrapped once as
the slice index is constructed and passed to
__getitem__, and again when item.indices is called. 
This makes it impossible to implement a correctly
behaving list data type without using hacks to suppress
this Python bug.

I would advise that you make the slices object passed
to __getitem__ not have its start/end indexes wrapped.

Connelly Barnes
msg60512 - (view) Author: Michael Hudson (mwh) (Python committer) Date: 2004-06-17 11:23
Logged In: YES 

You'll be happier if you make your classes new-style.

I don't know if it's worth changing old-style classes at
this point.  Personally, I'm trying to forget about them
just as quickly as possible :-)
msg60513 - (view) Author: Connelly (connelly) Date: 2004-06-17 21:50
Logged In: YES 

I'm not sure what you mean by 'make your classes new-style'.

According to, the 
__getitem__ method should be used, and the __getslice__ 
method is deprecated.

If you subclass the built-in list type, then the __getitem__ 
method is *not* called when subscripting with a slice.  
Instead, the __getslice__ method is called.  Try it out.

So I can't see any reasonable way to get around this bug.

You can try and modify the class C shown above, so that it 
behaves correctly.  I don't think you will be able to do it 
without putting in special "workaround" code to avoid this 
Python bug.

y = C([1, 2, 3])
>>> print y[-7:-5]         # should be [].

msg60514 - (view) Author: Michael Hudson (mwh) (Python committer) Date: 2004-06-18 10:46
Logged In: YES 

'make your classes new-style' == subclass object.

>>> class C(object):
...   def __getitem__(self, item): print item
...   def __len__(self): return 10
>>> C()[-3:-2]
slice(-3, -2, None)
msg64309 - (view) Author: Alexander Belopolsky (belopolsky) * (Python committer) Date: 2008-03-22 01:19
This looks like a duplicate of issue723806 which was closed as "won't 
msg82057 - (view) Author: Daniel Diniz (ajaksu2) * (Python triager) Date: 2009-02-14 13:58
Will close this one as a duplicate of #723806 unless someone springs to
its defense.
Date User Action Args
2022-04-11 14:56:04adminsetgithub: 40411
2009-02-16 20:54:49brett.cannonsetstatus: open -> closed
resolution: wont fix
2009-02-14 13:58:37ajaksu2setnosy: + ajaksu2
dependencies: + overintelligent slice() behavior on integers
messages: + msg82057
2008-03-22 01:19:06belopolskysetnosy: + belopolsky
type: behavior
messages: + msg64309
components: + Interpreter Core, - None
2004-06-17 10:22:29connellycreate