slicings: 3 questions

A

Alan G Isaac

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"?

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...

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

Thanks,
Alan Isaac
 
C

Chris Rebert

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
 
A

Alan G Isaac

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


Ah sure, I was misreading it.
It is just enabling NumPy style indexing.
I was assuming that it was describing a legitimate
way so slice sequences generally.

Thanks,
Alan
 
A

Alan G Isaac

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.

Slices seem somewhat more general than ranges.

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.

Or rather, it makes sense, but is empty.
But I take your point.

However, I would turn it around slightly and ask:
when is it not the case that
range(*slice(start,stop,step).indices(stop)) != range(stop,start,step)

If there are no interesting cases, then it seems
that range might derive from slice.
Just curious...

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.

Ah, this is new in Python 3 I take it?
I was not aware of this. Use case?

Thanks!
Alan
 
R

Robert Kern

Slices seem somewhat more general than ranges.



Or rather, it makes sense, but is empty.
But I take your point.

However, I would turn it around slightly and ask:
when is it not the case that
range(*slice(start,stop,step).indices(stop)) != range(stop,start,step)

If there are no interesting cases, then it seems
that range might derive from slice.
Just curious...

It's possible that it *could*, but there's also no real benefit to doing so.
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.

Ah, this is new in Python 3 I take it?

No.

Python 2.5.1 (r251:54869, Apr 18 2007, 22:08:04)
[GCC 4.0.1 (Apple Computer, Inc. build 5367)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
slice('a' said:
I was not aware of this. Use case?

It allows (ab)uses like numpy.mgrid:
array([ 0., 1., 2., 3., 4., 5., 6., 7., 8., 9., 10.])

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
 
G

Gabriel Genellina

On 1/29/2009 1:37 PM Chris Rebert apparently wrote:
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.

Ah, this is new in Python 3 I take it?

No.

[test using 2.5.1]

Just out of curiosity, I tried with the oldest Python I have:

Python 1.5.2 (#0, Apr 13 1999, 10:51:12) [MSC 32 bit (Intel)] on win32
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
slice('a', [8], 3j)

So, no, it's far for being a new feature...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top