Rationale behind the deprecation of __getslice__?
Steven Bethard
steven.bethard at gmail.com
Thu Dec 9 18:30:54 EST 2004
Fernando Perez wrote:
> I guess that conceptually it just felt natural to me to keep separate methods
> for dealing with a slice (get many elements out) and other types of indexing,
> which I tend to think of as 'scalar' indexing.
Yeah, I can see that a bit.
Ignoring dicts for the moment (and concerning ourselves only with
"sequences"), you're probably right in thinking that that slice objects
are the second most common thing to get in __getitem__ (second to ints
of course). But there is heavy use of other objects in various other
modules, most notably tuples in numarray:
>>> import numarray as na
>>> a = na.arange(24, shape=(2, 3, 4))
>>> a
array([[[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]],
[[12, 13, 14, 15],
[16, 17, 18, 19],
[20, 21, 22, 23]]])
>>> a[0,1,2]
6
>>> a[0,1]
array([4, 5, 6, 7])
>>> a[...,3]
array([[ 3, 7, 11],
[15, 19, 23]])
>>> a[1,:,0]
array([12, 16, 20])
Presumably the numarray code has to do quite a bit of type checking to
perform all these slicings right (and I didn't even show you what
happens when you use another array as an "index"). I'm not necessarily
saying that all this type checking is a good thing, but because people
will always find new things that they want to index by, adding
__getxxx__ methods for each of the index types is probably not the right
road to go down...
Steve
More information about the Python-list
mailing list