Hi all, 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).
The problem with this approach, besides a bit of code duplication, is that classes which implement slicing must now do runtime type-checking inside __getitem__. Here's a trivial example: ################################################################ import types class Vector(list): def __init__(self,arg,wrap=0): ''' If arg is an integer, make a new zero vector of length n. Otherwise, arg must be a python sequence or another vector, and a new (deep) copy is generated. ''' if isinstance(arg,int): list.__init__(self,[0.0]*arg) else: list.__init__(self,arg) def __getitem__(self,key): """called for single-element OR slice access""" if type(key) is types.SliceType: return Vector(list.__getitem__(self,key)) else: return list.__getitem__(self,key) def __getslice__(self,i,j): """Deprecated since 2.0, but still called for non-extended slices. Since extended slices are handled by __getitem__, I'm just deferring to that one so all actual implementation is done there. Why this is not the default (since they deprecated __getslice__) is beyond me.""" return self.__getitem__(slice(i,j)) print 'a is a vector' a = Vector(5) print a print type(a) print print 'b is a slice of a' b = a[1:3] print b print type(b) print print 'c is an element of a' c = a[1] print c print type(c) ################################################################ What bugs me is that typecheck for slicing which seems to be necessary inside of the __getitem__ method. I have the feeling that the code would be much cleaner if I could simply use __getslice__ for slices and __getitem__ for items, and that bundling the two in this hybrid mode is reall ugly and unpythonic. Am I just missing something? Thanks for any help, f -- http://mail.python.org/mailman/listinfo/python-list