3.3.5 Additional methods for emulation of sequence types

The following methods can be defined to further emulate sequence objects. Immutable sequences methods should only define __getslice__(); mutable sequences, should define all three three methods.

__getslice__ (self, i, j)
Deprecated since release 2.0. Support slice objects as parameters to the __getitem__() method.

Called to implement evaluation of self[i:j]. The returned object should be of the same type as self. Note that missing i or j in the slice expression are replaced by zero or sys.maxint, respectively. If negative indexes are used in the slice, the length of the sequence is added to that index. If the instance does not implement the __len__() method, an AttributeError is raised. No guarantee is made that indexes adjusted this way are not still negative. Indexes which are greater than the length of the sequence are not modified. If no __getslice__() is found, a slice object is created instead, and passed to __getitem__() instead.

__setslice__ (self, i, j, sequence)
Called to implement assignment to self[i:j]. Same notes for i and j as for __getslice__().

This method is deprecated. If no __setslice__() is found, a slice object is created instead, and passed to __setitem__() instead.

__delslice__ (self, i, j)
Called to implement deletion of self[i:j]. Same notes for i and j as for __getslice__(). This method is deprecated. If no __delslice__() is found, a slice object is created instead, and passed to __delitem__() instead.

Notice that these methods are only invoked when a single slice with a single colon is used, and the slice method is available. For slice operations involving extended slice notation, or in absence of the slice methods, __getitem__(), __setitem__() or __delitem__() is called with a slice object as argument.


See About this document... for information on suggesting changes.