From f89259786a166ab93417cdf442c96bc03183278b Mon Sep 17 00:00:00 2001 From: Fred Drake Date: Thu, 21 Sep 2000 22:27:16 +0000 Subject: [PATCH] Denis S. Otkidach : Show how code can be written to handle __getslice__ & friends in a way that is compatible with pre-2.0 versions of Python while still working with the "new" way of handling slicing. Additional explanation added by Fred Drake. This closes SourceForge patch #101388. --- Doc/ref/ref3.tex | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/Doc/ref/ref3.tex b/Doc/ref/ref3.tex index 6648d27d6a7..905a001784d 100644 --- a/Doc/ref/ref3.tex +++ b/Doc/ref/ref3.tex @@ -1173,6 +1173,48 @@ involving extended slice notation, or in absence of the slice methods, \method{__getitem__()}, \method{__setitem__()} or \method{__delitem__()} is called with a slice object as argument. +The following example demonstrate how to make your program or module +compatible with earlier versions of Python (assuming that methods +\method{__getitem__()}, \method{__setitem__()} and \method{__delitem__()} +support slice objects as arguments): + +\begin{verbatim} +class MyClass: + ... + def __getitem__(self, index): + ... + def __setitem__(self, index, value): + ... + def __delitem__(self, index): + ... + + if sys.version_info < (2, 0): + # They won't be defined if version is at least 2.0 final + + def __getslice__(self, i, j): + return self[max(0, i):max(0, j):] + def __setslice__(self, i, j, seq): + self[max(0, i):max(0, j):] = seq + def __delslice__(self, i, j): + del self[max(0, i):max(0, j):] + ... +\end{verbatim} + +Note the calls to \function{max()}; these are actually necessary due +to the handling of negative indices before the +\method{__*slice__()} methods are called. When negative indexes are +used, the \method{__*item__()} methods receive them as provided, but +the \method{__*slice__()} methods get a ``cooked'' form of the index +values. For each negative index value, the length of the sequence is +added to the index before calling the method (which may still result +in a negative index); this is the customary handling of negative +indexes by the built-in sequence types, and the \method{__*item__()} +methods are expected to do this as well. However, since they should +already be doing that, negative indexes cannot be passed in; they must +be be constrained to the bounds of the sequence before being passed to +the \method{__*item__()} methods. +Calling \code{max(0, i)} conveniently returns the proper value. + The membership test operators (\keyword{in} and \keyword{not in}) are normally implemented as iteration loop through the sequence. However, sequence objects can supply the following special method with a more