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)
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.

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

__delslice__ (self, i, j)
Called to implement deletion of self[i:j]. Same notes for i and j as for __getslice__().

Notice that these methods are only invoked when a single slice with a single colon is used. For slice operations involving extended slice notation, __getitem__(), __setitem__() or__delitem__() is called.


Send comments on this document to python-docs@python.org.