Skip to main content

Module slice

Module slice 

Source
Expand description

slice, which is what a subscript with a colon in it builds.

A slice holds three objects rather than three integers. x['a':'b'] builds one without complaint and only raises when a sequence tries to use it, which is why the check lives in Slice::indices rather than in the constructor.

Slice::indices is CPython’s PySlice_Unpack followed by PySlice_AdjustIndices, and the reason to follow it that closely is the clamping. Every out of range bound in Python is quietly pulled back to the end of the sequence rather than raising, so x[5:100] on a list of three is [] and x[-100:] is the whole list, and an index far larger than the machine can hold is pulled back too: x[2**100:] is [] rather than an OverflowError. Getting that wrong produces an exception where a program expected an empty list, which is the sort of difference that only shows up on somebody else’s input.

Structs§

Indices
A slice resolved against the length of a particular sequence.
Slice
slice(start, stop, step), holding whatever it was given.