Rationale behind the deprecation of __getslice__?
Fernando Perez
fperez528 at yahoo.com
Thu Dec 9 17:12:04 EST 2004
Steven Bethard wrote:
> Fernando Perez wrote:
>> I was wondering if someone can help me understand why __getslice__ has been
>> deprecated, yet it remains necessary to implement it for simple slices
>> (i:j), while __getitem__ gets called for extended slices (i:j:k).
>
> I don't think this is true -- everything goes to __getitem__:
>
> >>> class C(object):
> ... def __getitem__(self, *args, **kwds):
> ... print args, kwds
> ...
> >>> c = C()
> >>> c[1]
> (1,) {}
> >>> c[1:2]
> (slice(1, 2, None),) {}
> >>> c[1:2:-1]
> (slice(1, 2, -1),) {}
Not if you subclass builtin types like list:
In [6]: class mylist(list):
...: def __getitem__(self,*args,**kwds):
...: print 'mylist getitem'
...: print args,kwds
...:
In [7]: a=mylist()
In [8]: a[1]
mylist getitem
(1,) {}
In [9]: a[1:2]
Out[9]: []
In [10]: a[1:2:3]
mylist getitem
(slice(1, 2, 3),) {}
I did this testing, which is what forced me to implement __getslice__
separately in my little example, to satisfy calls with simple i:j slices.
Best,
f
More information about the Python-list
mailing list