# slicings: 3 questions

Chris Rebert clp2 at rebertia.com
Thu Jan 29 19:37:48 CET 2009

```On Thu, Jan 29, 2009 at 10:01 AM, Alan G Isaac <alan.isaac at gmail.com> wrote:
> 1. I seem not to understand something obvious at
> http://docs.python.org/3.0/reference/expressions.html#slicings
> (I assume I'm just not reading this right.)
> What is an example of a slicing using a "slice_list"?

There's nothing in the stdlib that uses it IIRC, but for instance a
matrix package could allow you to do:

a = Matrix(12,12,12) #make a 3D cubic matrix of all 0s
print( a[4,2,6] ) #get an item from the matrix using slice_list syntax
print( a[4][2][6] ) #another way to get the same item

> 2. It seems that slice objects and range objects are
> awfully similar in many ways.  Is this "appearance only",
> or was there any discussion of unifying them?
> Curious for insight...

Wouldn't really be possible, IMHO. True, they both have notions of
start, stop, and step, but slices don't make sense as ranges without
knowing the length of the container. For example, take the slice
`1:-2:1`. This is somewhat equivalent to range(1, -2, 1). However,
since  -2 < 1 (stop < start ) and 1 (the step) is positive, the range
is nonsensical.
You have to replace all the negative indices with calculated positive
indices first in order to have a sensical range(), in this case,
replacing the stop of -2 with len(container)-2.
Also, more fundamentally, Python is liberal in what it allows for the
parts of slices, so unifying slices with ranges would break code. For
example, Python is perfectly happy if I go slice("a",[8],object), none
of which are even numbers.

> 3. Why are slicings not directly listed among the built-in types?
> (I know they are discussed here:
> http://docs.python.org/3.0/reference/datamodel.html#types).
> What makes them "internal"?
> http://docs.python.org/3.0/library/stdtypes.html#internal-objects

Because they're generally only useful in the context of slicing lists
and other containers, and not as a datatype in and of themselves.
Also, the syntax sugar of a[b:c:d] means that you can practically
disregard the existence of slice() objects and just think that there's
special list-slicing syntax, with very little downside.

Cheers,
Chris

--
Follow the path of the Iguana...
http://rebertia.com

```