Rationale behind the deprecation of __getslice__?

Steven Bethard steven.bethard at gmail.com
Thu Dec 9 22:47:26 CET 2004


Fernando Perez wrote:
> classes which implement slicing must now do runtime type-checking inside
> __getitem__.

Just in case you thought that they wouldn't have to do runtime 
type-checking otherwise:

 >>> class C(object):
...     def __getitem__(self, x):
...         print type(x), x
...
 >>> c = C()
 >>> c[1]
<type 'int'> 1
 >>> c[1:2]
<type 'slice'> slice(1, 2, None)
 >>> c[1:2:-1]
<type 'slice'> slice(1, 2, -1)
 >>> c[1,2]
<type 'tuple'> (1, 2)
 >>> c[1,2:3]
<type 'tuple'> (1, slice(2, 3, None))
 >>> c['1']
<type 'str'> 1
 >>> c[[]]
<type 'list'> []

You can put just about anything in a __getitem__ call.  Do you really 
want a method for each of the variants above?

Steve



More information about the Python-list mailing list