summaryrefslogtreecommitdiffstats
path: root/Doc/whatsnew
diff options
context:
space:
mode:
authorMichael W. Hudson <mwh@python.net>2002-07-19 15:48:56 (GMT)
committerMichael W. Hudson <mwh@python.net>2002-07-19 15:48:56 (GMT)
commit4da01ed9a8fa523b31b4b42f092571d41002e066 (patch)
tree4acf6ce500833c10ff185621f1449e23e461f3d5 /Doc/whatsnew
parentf0d777c56b710f68c96582c10b49d83b5ad7d32d (diff)
downloadcpython-4da01ed9a8fa523b31b4b42f092571d41002e066.zip
cpython-4da01ed9a8fa523b31b4b42f092571d41002e066.tar.gz
cpython-4da01ed9a8fa523b31b4b42f092571d41002e066.tar.bz2
Substantially flesh out extended slice section. I think this is probably
done now.
Diffstat (limited to 'Doc/whatsnew')
-rw-r--r--Doc/whatsnew/whatsnew23.tex83
1 files changed, 83 insertions, 0 deletions
diff --git a/Doc/whatsnew/whatsnew23.tex b/Doc/whatsnew/whatsnew23.tex
index a854f20..5ba86d8 100644
--- a/Doc/whatsnew/whatsnew23.tex
+++ b/Doc/whatsnew/whatsnew23.tex
@@ -370,7 +370,90 @@ This also works for strings:
'dcba'
\end{verbatim}
+as well as tuples and arrays.
+If you have a mutable sequence (i.e. a list or an array) you can
+assign to or delete an extended slice, but there are some differences
+in assignment to extended and regular slices. Assignment to a regular
+slice can be used to change the length of the sequence:
+
+\begin{verbatim}
+>>> a = range(3)
+>>> a
+[0, 1, 2]
+>>> a[1:3] = [4, 5, 6]
+>>> a
+[0, 4, 5, 6]
+\end{verbatim}
+
+but when assigning to an extended slice the list on the right hand
+side of the statement must contain the same number of items as the
+slice it is replacing:
+
+\begin{verbatim}
+>>> a = range(4)
+>>> a
+[0, 1, 2, 3]
+>>> a[::2]
+[0, 2]
+>>> a[::2] = range(0, -2, -1)
+>>> a
+[0, 1, -1, 3]
+>>> a[::2] = range(3)
+Traceback (most recent call last):
+ File "<stdin>", line 1, in ?
+ValueError: attempt to assign list of size 3 to extended slice of size 2
+\end{verbatim}
+
+Deletion is more straightforward:
+
+\begin{verbatim}
+>>> a = range(4)
+>>> a[::2]
+[0, 2]
+>>> del a[::2]
+>>> a
+[1, 3]
+\end{verbatim}
+
+One can also now pass slice objects to builtin sequences
+\method{__getitem__} methods:
+
+\begin{verbatim}
+>>> range(10).__getitem__(slice(0, 5, 2))
+[0, 2, 4]
+\end{verbatim}
+
+or use them directly in subscripts:
+
+\begin{verbatim}
+>>> range(10)[slice(0, 5, 2)]
+[0, 2, 4]
+\end{verbatim}
+
+To make implementing sequences that support extended slicing in Python
+easier, slice ojects now have a method \method{indices} which given
+the length of a sequence returns \code{(start, stop, step)} handling
+omitted and out-of-bounds indices in a manner consistent with regular
+slices (and this innocuous phrase hides a welter of confusing
+details!). The method is intended to be used like this:
+
+\begin{verbatim}
+class FakeSeq:
+ ...
+ def calc_item(self, i):
+ ...
+ def __getitem__(self, item):
+ if isinstance(item, slice):
+ return FakeSeq([self.calc_item(i)
+ in range(*item.indices(len(self)))])
+ else:
+ return self.calc_item(i)
+\end{verbatim}
+
+From this example you can also see that the builtin ``\var{slice}''
+object is now the type of slice objects, not a function (so is now
+consistent with \var{int}, \var{str}, etc from 2.2).
%======================================================================
\section{Other Language Changes}