cpython: 31875b244bdf (original) (raw)
Mercurial > cpython
changeset 93821:31875b244bdf 3.4
Issue #23006: Improve the documentation and indexing of dict.__missing__. Add an entry in the language datamodel special methods section. Revise and index its discussion in the stdtypes mapping/dict section. [#23006]
Terry Jan Reedy tjreedy@udel.edu | |
---|---|
date | Wed, 10 Dec 2014 18:38:19 -0500 |
parents | d04dab84388f |
children | dd5491705e5f 41b172fd4479 |
files | Doc/library/stdtypes.rst Doc/reference/datamodel.rst Misc/NEWS |
diffstat | 3 files changed, 18 insertions(+), 5 deletions(-)[+] [-] Doc/library/stdtypes.rst 13 Doc/reference/datamodel.rst 6 Misc/NEWS 4 |
line wrap: on
line diff
--- a/Doc/library/stdtypes.rst
+++ b/Doc/library/stdtypes.rst
@@ -3761,11 +3761,13 @@ pairs within braces, for example: ``{'ja
Return the item of d with key key. Raises a :exc:KeyError
if key is
not in the map.
If a subclass of dict defines a method :meth:`__missing__`, if the key *key*[](#l1.7)
.. index:: __missing__()[](#l1.8)
If a subclass of dict defines a method :meth:`__missing__` and *key*[](#l1.10) is not present, the ``d[key]`` operation calls that method with the key *key*[](#l1.11) as argument. The ``d[key]`` operation then returns or raises whatever is[](#l1.12)
returned or raised by the ``__missing__(key)`` call if the key is not[](#l1.13)
present. No other operations or methods invoke :meth:`__missing__`. If[](#l1.14)
returned or raised by the ``__missing__(key)`` call.[](#l1.15)
No other operations or methods invoke :meth:`__missing__`. If[](#l1.16) :meth:`__missing__` is not defined, :exc:`KeyError` is raised.[](#l1.17) :meth:`__missing__` must be a method; it cannot be an instance variable::[](#l1.18)
@@ -3779,8 +3781,9 @@ pairs within braces, for example: ``{'ja >>> c['red'] 1
See :class:`collections.Counter` for a complete implementation including[](#l1.24)
other methods helpful for accumulating and managing tallies.[](#l1.25)
The example above shows part of the implementation of[](#l1.26)
:class:`collections.Counter`. A different ``__missing__`` method is used[](#l1.27)
by :class:`collections.defaultdict`.[](#l1.28)
--- a/Doc/reference/datamodel.rst +++ b/Doc/reference/datamodel.rst @@ -1904,6 +1904,12 @@ through the container; for mappings, :me indexes to allow proper detection of the end of the sequence. +.. method:: object.missing(self, key) +
- Called by :class:
dict
\ .\ :meth:__getitem__
to implementself[key]
for dict subclasses - when key is not in the dictionary. +
+
.. method:: object.setitem(self, key, value)
Called to implement assignment to self[key]
. Same note as for
--- a/Misc/NEWS +++ b/Misc/NEWS @@ -9474,6 +9474,10 @@ C-API Documentation ------------- +- Issue #23006: Improve the documentation and indexing of dict.missing.