diff options
author | Fred Drake <fdrake@acm.org> | 2000-09-21 22:27:16 (GMT) |
---|---|---|
committer | Fred Drake <fdrake@acm.org> | 2000-09-21 22:27:16 (GMT) |
commit | f89259786a166ab93417cdf442c96bc03183278b (patch) | |
tree | df42046fae6c740a15a8e4e5fb5a0fc79eec8dc2 /Doc/ref | |
parent | 1a5e5830a7121b92d029c94feea0984b6aa1022f (diff) | |
download | cpython-f89259786a166ab93417cdf442c96bc03183278b.zip cpython-f89259786a166ab93417cdf442c96bc03183278b.tar.gz cpython-f89259786a166ab93417cdf442c96bc03183278b.tar.bz2 |
Denis S. Otkidach <ods@users.sourceforge.net>:
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.
Diffstat (limited to 'Doc/ref')
-rw-r--r-- | Doc/ref/ref3.tex | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/Doc/ref/ref3.tex b/Doc/ref/ref3.tex index 6648d27..905a001 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 |