Why is __getslice__ still implemented?

Jean-Paul Calderone exarkun at divmod.com
Tue Apr 10 17:59:23 CEST 2007


On Tue, 10 Apr 2007 09:51:45 -0600, Steven Bethard <steven.bethard at gmail.com> wrote:
>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'>
>

Ah, excellent point.

Jean-Paul



More information about the Python-list mailing list