[Python-Dev] slice subscripts for sequences and mappings

Eli Bendersky eliben at gmail.com
Sat Mar 3 13:07:35 CET 2012


On Sat, Mar 3, 2012 at 11:24, Stefan Behnel <stefan_ml at behnel.de> wrote:
> Eli Bendersky, 03.03.2012 09:36:
>> I find a strange discrepancy in Python with regards to slice
>> subscripting of objects, at the C API level. I mean things like
>> obj[start:end:step].
>>
>> I'd expect slice subscripts to be part of the sequence interface, and
>> yet they are not. In fact, they are part of the mapping interface. For
>> example, the list object has its slice get/set methods assigned to a
>> PyMappingMethods struct. So does a bytes object, and pretty much every
>> other object that wants to support subscripts.
>>
>> This doesn't align well with the documentation, in at least two places.
>>
>> 1) The library documentation
>> (http://docs.python.org/dev/library/stdtypes.html) in 4.8 says:
>>
>>     "Mappings are mutable objects. There is currently only one
>> standard mapping type, the dictionary"
>>
>> Why then does a list implement the mapping interface? Moreover, why
>> does bytes, an immutable object, implement the mapping interface?
>
> I think that's (partly?) for historical reasons. Originally, there were the
> slicing functions as part of the sequence interface. They took a start and
> an end index of the slice. Then, extended slicing was added to the
> language, and that used a slice object, which didn't fit into the sequence
> slicing interface. So the interface was unified using the existing mapping
> getitem interface, and the sequence slicing functions were eventually
> deprecated and removed in Py3.

This make sense. Not that now there's also duplication in almost all
objects because the mapping protocol essentially supersedes the
sequence protocol for accessing elements. I.e. sq_item and sq_ass_item
are no longer needed if an object implements the mapping protocol,
because the mapping interface has precedence, and mp_subscript &
mp_ass_subscript are called instead, respectively. Because of that,
the first thing they do is check whether the index is a simple number
and do the work of their sequence protocol cousins. This duplicates
code in almost all objects that need to support __getitem__.

Eli


More information about the Python-Dev mailing list