]> granicus.if.org Git - python/commitdiff
Issue #23006: Improve the documentation and indexing of dict.__missing__.
authorTerry Jan Reedy <tjreedy@udel.edu>
Wed, 10 Dec 2014 23:38:07 +0000 (18:38 -0500)
committerTerry Jan Reedy <tjreedy@udel.edu>
Wed, 10 Dec 2014 23:38:07 +0000 (18:38 -0500)
Add an entry in the language datamodel special methods section.
Revise and index its discussion in the stdtypes mapping/dict section.
Backport the code example from 3.4.

Doc/library/stdtypes.rst
Doc/reference/datamodel.rst
Misc/NEWS

index fa4304cffd80c037defd85d9047d4918249fbe4c..bc437a4ae45cdb95dc5a1156418e824c05fbd555 100644 (file)
@@ -2031,16 +2031,32 @@ pairs within braces, for example: ``{'jack': 4098, 'sjoerd': 4127}`` or ``{4098:
       Return the item of *d* with key *key*.  Raises a :exc:`KeyError` if *key*
       is not in the map.
 
+      .. index:: __missing__()
+
+      If a subclass of dict defines a method :meth:`__missing__` and *key*
+      is not present, the ``d[key]`` operation calls that method with the key *key*
+      as argument.  The ``d[key]`` operation then returns or raises whatever is
+      returned or raised by the ``__missing__(key)`` call.
+      No other operations or methods invoke :meth:`__missing__`. If
+      :meth:`__missing__` is not defined, :exc:`KeyError` is raised.
+      :meth:`__missing__` must be a method; it cannot be an instance variable::
+
+          >>> class Counter(dict):
+          ...     def __missing__(self, key):
+          ...         return 0
+          >>> c = Counter()
+          >>> c['red']
+          0
+          >>> c['red'] += 1
+          >>> c['red']
+          1
+
+      The example above shows part of the implementation of
+      :class:`collections.Counter`.  A different ``__missing__`` method is used
+      by :class:`collections.defaultdict`.
+
       .. versionadded:: 2.5
-         If a subclass of dict defines a method :meth:`__missing__`, if the key
-         *key* is not present, the ``d[key]`` operation calls that method with
-         the key *key* as argument.  The ``d[key]`` operation then returns or
-         raises whatever is returned or raised by the ``__missing__(key)`` call
-         if the key is not present. No other operations or methods invoke
-         :meth:`__missing__`. If :meth:`__missing__` is not defined,
-         :exc:`KeyError` is raised.  :meth:`__missing__` must be a method; it
-         cannot be an instance variable. For an example, see
-         :class:`collections.defaultdict`.
+         Recognition of __missing__ methods of dict subclasses.
 
    .. describe:: d[key] = value
 
index 206853b27d990e2ae93cb2c94361879300403e71..5a6c763769b031f16357bb0cb0afb6b1f7443f02 100644 (file)
@@ -1904,6 +1904,12 @@ sequences, it should iterate through the values.
       indexes to allow proper detection of the end of the sequence.
 
 
+.. method:: object.__missing__(self, key)
+
+   Called by :class:`dict`\ .\ :meth:`__getitem__` to implement ``self[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
index 6d0f3eb7a709167287648e09ecd77eb2b475946e..6f4e5521f8eaee9d9d63f4904b5cb695a05df30a 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -30,6 +30,11 @@ Library
 Documentation
 -------------
 
+- 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.
+  Backport the code example from 3.4.
+
 - Issue #21514: The documentation of the json module now refers to new JSON RFC
   7159 instead of obsoleted RFC 4627.