Why is __getslice__ still implemented?

Steven Bethard steven.bethard at gmail.com
Tue Apr 10 11:51:45 EDT 2007


Jean-Paul Calderone wrote:
> On Tue, 10 Apr 2007 08:35:56 -0600, Steven Bethard 
> <steven.bethard at gmail.com> wrote:
>> Yes, you do still need to implement __getslice__ if you're subclassing
>> a class (like unicode or list) which provides it. The __getslice__
>> method can't be removed entirely for backwards compatibility reasons
>> (though it is being removed in Python 3000).
> 
> Why does this mean that the unicode type has to implement __getslice__?

Because code could exist like::

     >>> class C(list):
     ...     def __getslice__(self, start, stop):
     ...         return C(list.__getslice__(self, start, stop))
     ...
     >>> type(C([1, 2, 3, 4])[:2])
     <class '__main__.C'>

For similar examples, see:

     http://www.google.com/codesearch?q=list.__getslice__

I couldn't find any real instances of unicode.__getslice__:

     http://www.google.com/codesearch?q=unicode.__getslice__

But I suspect python-dev wouldn't think it was worth it to remove just 
unicode.__getslice__ and not all the other ones...

STeVe



More information about the Python-list mailing list