[Python-checkins] CVS: python/dist/src/Doc/ref ref3.tex,1.51,1.52
Fred L. Drake
python-dev@python.org
Thu, 21 Sep 2000 15:27:19 -0700
Update of /cvsroot/python/python/dist/src/Doc/ref
In directory slayer.i.sourceforge.net:/tmp/cvs-serv25641/ref
Modified Files:
ref3.tex
Log Message:
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.
Index: ref3.tex
===================================================================
RCS file: /cvsroot/python/python/dist/src/Doc/ref/ref3.tex,v
retrieving revision 1.51
retrieving revision 1.52
diff -C2 -r1.51 -r1.52
*** ref3.tex 2000/09/21 05:28:26 1.51
--- ref3.tex 2000/09/21 22:27:16 1.52
***************
*** 1174,1177 ****
--- 1174,1219 ----
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,