Skip to content

Commit cb8ecb1

Browse files
committed
Doc update for __xslice__ removal.
1 parent 4a7b5d5 commit cb8ecb1

4 files changed

Lines changed: 57 additions & 147 deletions

File tree

Doc/library/constants.rst

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ A small number of constants live in the built-in namespace. They are:
3434
.. data:: Ellipsis
3535

3636
The same as ``...``. Special value used mostly in conjunction with extended
37-
slicing syntax for user-defined container data types.
38-
39-
.. % XXX Someone who understands extended slicing should fill in here.
37+
slicing syntax for user-defined container data types, as in ::
4038

39+
val = container[1:5, 7:10, ...]

Doc/library/stdtypes.rst

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -587,7 +587,6 @@ of the same type and have the same length. (For full details see
587587
pair: repetition; operation
588588
pair: subscript; operation
589589
pair: slice; operation
590-
pair: extended slice; operation
591590
operator: in
592591
operator: not in
593592

@@ -948,9 +947,10 @@ the :mod:`re` module for string functions based on regular expressions.
948947
.. method:: str.translate(map)
949948

950949
Return a copy of the *s* where all characters have been mapped through the
951-
*map* which must be a mapping of Unicode ordinals (integers) to Unicode
952-
ordinals, strings or ``None``. Unmapped characters are left
953-
untouched. Characters mapped to ``None`` are deleted.
950+
*map* which must be a dictionary of characters (strings of length 1) or
951+
Unicode ordinals (integers) to Unicode ordinals, strings or ``None``.
952+
Unmapped characters are left untouched. Characters mapped to ``None`` are
953+
deleted.
954954

955955
.. note::
956956

@@ -1244,7 +1244,6 @@ Note that while lists allow their items to be of any type, bytes object
12441244
triple: operations on; list; type
12451245
pair: subscript; assignment
12461246
pair: slice; assignment
1247-
pair: extended slice; assignment
12481247
statement: del
12491248
single: append() (sequence method)
12501249
single: extend() (sequence method)
@@ -2389,8 +2388,8 @@ It is written as ``None``.
23892388
The Ellipsis Object
23902389
-------------------
23912390

2392-
This object is mostly used by extended slice notation (see :ref:`slicings`). It
2393-
supports no special operations. There is exactly one ellipsis object, named
2391+
This object is commonly used by slicing (see :ref:`slicings`). It supports no
2392+
special operations. There is exactly one ellipsis object, named
23942393
:const:`Ellipsis` (a built-in name).
23952394

23962395
It is written as ``Ellipsis`` or ``...``.

Doc/reference/datamodel.rst

Lines changed: 43 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -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
15921587
either to emulate a sequence or to emulate a mapping; the difference is that for
15931588
a sequence, the allowable keys should be the integers *k* for which ``0 <= k <
15941589
N`` 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

17871700
Emulating numeric types

Doc/reference/expressions.rst

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -517,25 +517,24 @@ or list). Slicings may be used as expressions or as targets in assignment or
517517
simple_slicing: `primary` "[" `short_slice` "]"
518518
extended_slicing: `primary` "[" `slice_list` "]"
519519
slice_list: `slice_item` ("," `slice_item`)* [","]
520-
slice_item: `expression` | `proper_slice` | `ellipsis`
520+
slice_item: `expression` | `proper_slice`
521521
proper_slice: `short_slice` | `long_slice`
522522
short_slice: [`lower_bound`] ":" [`upper_bound`]
523523
long_slice: `short_slice` ":" [`stride`]
524524
lower_bound: `expression`
525525
upper_bound: `expression`
526526
stride: `expression`
527-
ellipsis: "..."
528-
529-
.. index:: pair: extended; slicing
530527

531528
There is ambiguity in the formal syntax here: anything that looks like an
532529
expression list also looks like a slice list, so any subscription can be
533530
interpreted as a slicing. Rather than further complicating the syntax, this is
534531
disambiguated by defining that in this case the interpretation as a subscription
535532
takes priority over the interpretation as a slicing (this is the case if the
536-
slice list contains no proper slice nor ellipses). Similarly, when the slice
537-
list has exactly one short slice and no trailing comma, the interpretation as a
538-
simple slicing takes priority over that as an extended slicing.
533+
slice list contains no proper slice). Similarly, when the slice list has
534+
exactly one short slice and no trailing comma, the interpretation as a simple
535+
slicing takes priority over that as an extended slicing.
536+
537+
.. XXX is the next paragraph stil correct?
539538
540539
The semantics for a simple slicing are as follows. The primary must evaluate to
541540
a sequence object. The lower and upper bound expressions, if present, must

0 commit comments

Comments
 (0)