@@ -265,8 +265,6 @@ Sequences
265265 sequence of the same type. This implies that the index set is renumbered so
266266 that it starts at 0.
267267
268- .. index :: single: extended slicing
269-
270268 Some sequences also support "extended slicing" with a third "step" parameter:
271269 ``a[i:j:k] `` selects all items of *a * with index *x * where ``x = i + n*k ``, *n *
272270 ``>= `` ``0 `` and *i * ``<= `` *x * ``< `` *j *.
@@ -997,10 +995,8 @@ Internal types
997995 Slice objects
998996 .. index :: builtin: slice
999997
1000- Slice objects are used to represent slices when *extended slice syntax * is used.
1001- This is a slice using two colons, or multiple slices or ellipses separated by
1002- commas, e.g., ``a[i:j:step] ``, ``a[i:j, k:l] ``, or ``a[..., i:j] ``. They are
1003- also created by the built-in :func: `slice ` function.
998+ Slice objects are used to represent slices for :meth: `__getitem__ `
999+ methods. They are also created by the built-in :func: `slice ` function.
10041000
10051001 .. index ::
10061002 single: start (slice object attribute)
@@ -1013,15 +1009,14 @@ Internal types
10131009
10141010 Slice objects support one method:
10151011
1016-
10171012 .. method :: slice.indices(self, length)
10181013
1019- This method takes a single integer argument *length * and computes information
1020- about the extended slice that the slice object would describe if applied to a
1021- sequence of *length * items. It returns a tuple of three integers; respectively
1022- these are the *start * and *stop * indices and the * step * or stride length of the
1023- slice. Missing or out-of-bounds indices are handled in a manner consistent with
1024- regular slices.
1014+ This method takes a single integer argument *length * and computes
1015+ information about the slice that the slice object would describe if
1016+ applied to a sequence of *length * items. It returns a tuple of three
1017+ integers; respectively these are the *start * and *stop * indices and the
1018+ * step * or stride length of the slice. Missing or out-of-bounds indices
1019+ are handled in a manner consistent with regular slices.
10251020
10261021 Static method objects
10271022 Static method objects provide a way of defeating the transformation of function
@@ -1592,31 +1587,28 @@ but can represent other containers as well. The first set of methods is used
15921587either to emulate a sequence or to emulate a mapping; the difference is that for
15931588a sequence, the allowable keys should be the integers *k * for which ``0 <= k <
15941589N `` where *N * is the length of the sequence, or slice objects, which define a
1595- range of items. (For backwards compatibility, the method :meth: `__getslice__ `
1596- (see below) can also be defined to handle simple, but not extended slices.) It
1597- is also recommended that mappings provide the methods :meth: `keys `,
1598- :meth: `values `, :meth: `items `, :meth: `has_key `, :meth: `get `, :meth: `clear `,
1599- :meth: `setdefault `, :meth: `iterkeys `, :meth: `itervalues `, :meth: `iteritems `,
1600- :meth: `pop `, :meth: `popitem `, :meth: `copy `, and :meth: `update ` behaving similar
1601- to those for Python's standard dictionary objects. The :mod: `UserDict ` module
1602- provides a :class: `DictMixin ` class to help create those methods from a base set
1603- of :meth: `__getitem__ `, :meth: `__setitem__ `, :meth: `__delitem__ `, and
1604- :meth: `keys `. Mutable sequences should provide methods :meth: `append `,
1605- :meth: `count `, :meth: `index `, :meth: `extend `, :meth: `insert `, :meth: `pop `,
1606- :meth: `remove `, :meth: `reverse ` and :meth: `sort `, like Python standard list
1607- objects. Finally, sequence types should implement addition (meaning
1608- concatenation) and multiplication (meaning repetition) by defining the methods
1609- :meth: `__add__ `, :meth: `__radd__ `, :meth: `__iadd__ `, :meth: `__mul__ `,
1610- :meth: `__rmul__ ` and :meth: `__imul__ ` described below; they should not define
1611- other numerical operators. It is recommended that both mappings and sequences
1612- implement the :meth: `__contains__ ` method to allow efficient use of the ``in ``
1613- operator; for mappings, ``in `` should be equivalent of :meth: `has_key `; for
1614- sequences, it should search through the values. It is further recommended that
1615- both mappings and sequences implement the :meth: `__iter__ ` method to allow
1616- efficient iteration through the container; for mappings, :meth: `__iter__ ` should
1617- be the same as :meth: `iterkeys `; for sequences, it should iterate through the
1618- values.
1619-
1590+ range of items. It is also recommended that mappings provide the methods
1591+ :meth: `keys `, :meth: `values `, :meth: `items `, :meth: `has_key `, :meth: `get `,
1592+ :meth: `clear `, :meth: `setdefault `, :meth: `iterkeys `, :meth: `itervalues `,
1593+ :meth: `iteritems `, :meth: `pop `, :meth: `popitem `, :meth: `copy `, and
1594+ :meth: `update ` behaving similar to those for Python's standard dictionary
1595+ objects. The :mod: `UserDict ` module provides a :class: `DictMixin ` class to help
1596+ create those methods from a base set of :meth: `__getitem__ `,
1597+ :meth: `__setitem__ `, :meth: `__delitem__ `, and :meth: `keys `. Mutable sequences
1598+ should provide methods :meth: `append `, :meth: `count `, :meth: `index `,
1599+ :meth: `extend `, :meth: `insert `, :meth: `pop `, :meth: `remove `, :meth: `reverse ` and
1600+ :meth: `sort `, like Python standard list objects. Finally, sequence types should
1601+ implement addition (meaning concatenation) and multiplication (meaning
1602+ repetition) by defining the methods :meth: `__add__ `, :meth: `__radd__ `,
1603+ :meth: `__iadd__ `, :meth: `__mul__ `, :meth: `__rmul__ ` and :meth: `__imul__ `
1604+ described below; they should not define other numerical operators. It is
1605+ recommended that both mappings and sequences implement the :meth: `__contains__ `
1606+ method to allow efficient use of the ``in `` operator; for mappings, ``in ``
1607+ should be equivalent of :meth: `has_key `; for sequences, it should search through
1608+ the values. It is further recommended that both mappings and sequences
1609+ implement the :meth: `__iter__ ` method to allow efficient iteration through the
1610+ container; for mappings, :meth: `__iter__ ` should be the same as
1611+ :meth: `iterkeys `; for sequences, it should iterate through the values.
16201612
16211613.. method :: object.__len__(self)
16221614
@@ -1630,6 +1622,19 @@ values.
16301622 considered to be false in a Boolean context.
16311623
16321624
1625+ .. note ::
1626+
1627+ Slicing is done exclusively with the following three methods. A call like ::
1628+
1629+ a[1:2] = b
1630+
1631+ is translated to ::
1632+
1633+ a[slice(1, 2, None)] = b
1634+
1635+ and so forth. Missing slice items are always filled in with ``None ``.
1636+
1637+
16331638.. method :: object.__getitem__(self, key)
16341639
16351640 .. index :: object: slice
@@ -1690,98 +1695,6 @@ also does not require the object be a sequence.
16901695 of the mapping rather than the values or the key-item pairs.
16911696
16921697
1693- .. _sequence-methods :
1694-
1695- Additional methods for emulation of sequence types
1696- --------------------------------------------------
1697-
1698- The following optional methods can be defined to further emulate sequence
1699- objects. Immutable sequences methods should at most only define
1700- :meth: `__getslice__ `; mutable sequences might define all three methods.
1701-
1702-
1703- .. method :: object.__getslice__(self, i, j)
1704-
1705- .. deprecated :: 2.0
1706- Support slice objects as parameters to the :meth: `__getitem__ ` method.
1707- (However, built-in types in CPython currently still implement
1708- :meth: `__getslice__ `. Therefore, you have to override it in derived
1709- classes when implementing slicing.)
1710-
1711- Called to implement evaluation of ``self[i:j] ``. The returned object should be
1712- of the same type as *self *. Note that missing *i * or *j * in the slice
1713- expression are replaced by zero or ``sys.maxint ``, respectively. If negative
1714- indexes are used in the slice, the length of the sequence is added to that
1715- index. If the instance does not implement the :meth: `__len__ ` method, an
1716- :exc: `AttributeError ` is raised. No guarantee is made that indexes adjusted this
1717- way are not still negative. Indexes which are greater than the length of the
1718- sequence are not modified. If no :meth: `__getslice__ ` is found, a slice object
1719- is created instead, and passed to :meth: `__getitem__ ` instead.
1720-
1721-
1722- .. method :: object.__setslice__(self, i, j, sequence)
1723-
1724- Called to implement assignment to ``self[i:j] ``. Same notes for *i * and *j * as
1725- for :meth: `__getslice__ `.
1726-
1727- This method is deprecated. If no :meth: `__setslice__ ` is found, or for extended
1728- slicing of the form ``self[i:j:k] ``, a slice object is created, and passed to
1729- :meth: `__setitem__ `, instead of :meth: `__setslice__ ` being called.
1730-
1731-
1732- .. method :: object.__delslice__(self, i, j)
1733-
1734- Called to implement deletion of ``self[i:j] ``. Same notes for *i * and *j * as for
1735- :meth: `__getslice__ `. This method is deprecated. If no :meth: `__delslice__ ` is
1736- found, or for extended slicing of the form ``self[i:j:k] ``, a slice object is
1737- created, and passed to :meth: `__delitem__ `, instead of :meth: `__delslice__ `
1738- being called.
1739-
1740- Notice that these methods are only invoked when a single slice with a single
1741- colon is used, and the slice method is available. For slice operations
1742- involving extended slice notation, or in absence of the slice methods,
1743- :meth: `__getitem__ `, :meth: `__setitem__ ` or :meth: `__delitem__ ` is called with a
1744- slice object as argument.
1745-
1746- The following example demonstrate how to make your program or module compatible
1747- with earlier versions of Python (assuming that methods :meth: `__getitem__ `,
1748- :meth: `__setitem__ ` and :meth: `__delitem__ ` support slice objects as
1749- arguments)::
1750-
1751- class MyClass:
1752- ...
1753- def __getitem__(self, index):
1754- ...
1755- def __setitem__(self, index, value):
1756- ...
1757- def __delitem__(self, index):
1758- ...
1759-
1760- if sys.version_info < (2, 0):
1761- # They won't be defined if version is at least 2.0 final
1762-
1763- def __getslice__(self, i, j):
1764- return self[max(0, i):max(0, j):]
1765- def __setslice__(self, i, j, seq):
1766- self[max(0, i):max(0, j):] = seq
1767- def __delslice__(self, i, j):
1768- del self[max(0, i):max(0, j):]
1769- ...
1770-
1771- Note the calls to :func: `max `; these are necessary because of the handling of
1772- negative indices before the :meth: `__\* slice__ ` methods are called. When
1773- negative indexes are used, the :meth: `__\* item__ ` methods receive them as
1774- provided, but the :meth: `__\* slice__ ` methods get a "cooked" form of the index
1775- values. For each negative index value, the length of the sequence is added to
1776- the index before calling the method (which may still result in a negative
1777- index); this is the customary handling of negative indexes by the built-in
1778- sequence types, and the :meth: `__\* item__ ` methods are expected to do this as
1779- well. However, since they should already be doing that, negative indexes cannot
1780- be passed in; they must be constrained to the bounds of the sequence before
1781- being passed to the :meth: `__\* item__ ` methods. Calling ``max(0, i) ``
1782- conveniently returns the proper value.
1783-
1784-
17851698.. _numeric-types :
17861699
17871700Emulating numeric types
0 commit comments