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.

classification
Title: __getslice__ still called
Type: behavior Stage:
Components: Interpreter Core Versions: Python 2.3, Python 2.5
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: mark.dickinson, stefan
Priority: normal Keywords:

Created on 2008-02-07 18:19 by stefan, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (4)
msg62162 - (view) Author: Stefan Seefeld (stefan) * (Python committer) Date: 2008-02-07 18:19
The python documentation states that since python 2.0 __getslice__ is
obsoleted by __getitem__. However, testing with python 2.3 as well as
2.5, I find the following surprising behavior:

class Tuple(tuple):

  def __getitem__(self, i): print '__getitem__', i
  def __getslice__(self, i): print '__getslice__', i

t = Tuple()
t[0] # __getitem__ called with type(i) == int
t[0:2] # __getslice__ called with type(i) == slice
t[0:2:1] # __getitem__ called with type(i) == slice
msg62166 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2008-02-07 18:44
I think the docs do a good job of explaining this;  in particular, they 
say, 
in http://docs.python.org/dev/reference/datamodel.html#special-method-
names:

"However, built-in types in CPython currently still implement 
__getslice__()."

and explain that __getslice__ is used only to implement the form a[i:j], 
and 
falls back to __getitem__ if __getslice__ is not implemented.

Getting rid of __getslice__ for builtin types in Python 2.x is probably 
one 
of those things that would break backwards compatibility.  And leaving 
it in 
is pretty harmless.  To avoid surprises, don't implement __getslice__ in 
your 
own classes.  But note that __getslice__ and friends are gone in Python 
3.0.

I'd recommend closing this as invalid, but I'll wait for a second 
opinion.
msg62168 - (view) Author: Stefan Seefeld (stefan) * (Python committer) Date: 2008-02-07 18:55
Mark,

thanks for the quick follow-up.
OK, i now understand the situation better. The documentation I had read 
originally didn't talk about special-casing built-in objects. (And since 
I want to extend a tuple, I do have to override __getslice__ since I 
want to make sure the returned object still has the derived type.)

Yes, I believe this issue can be closed as invalid.
(Though I believe the docs could be a bit more clear about this.)

Thanks,
		Stefan

-- 

       ...ich hab' noch einen Koffer in Berlin...
msg62170 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2008-02-07 19:09
Well, documentation patches are always welcome, I believe :)

If you can point to a particular place in the documentation and suggest 
alternative (or extra) wording that might help, post it here and I'll deal 
with it.
History
Date User Action Args
2022-04-11 14:56:30adminsetgithub: 46317
2008-02-07 19:09:56mark.dickinsonsetstatus: open -> closed
resolution: not a bug
messages: + msg62170
2008-02-07 18:55:25stefansetmessages: + msg62168
2008-02-07 18:44:22mark.dickinsonsetnosy: + mark.dickinson
messages: + msg62166
2008-02-07 18:19:20stefancreate