[Python-Dev] slice subscripts for sequences and mappings

Stefan Behnel stefan_ml at behnel.de
Sat Mar 3 10:24:34 CET 2012


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.

Stefan



More information about the Python-Dev mailing list