mirror of
https://github.com/python/cpython.git
synced 2025-11-03 19:34:08 +00:00
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.
This commit is contained in:
parent
60599525c5
commit
b67f6e27e1
3 changed files with 18 additions and 5 deletions
|
|
@ -3761,11 +3761,13 @@ 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
|
Return the item of *d* with key *key*. Raises a :exc:`KeyError` if *key* is
|
||||||
not in the map.
|
not in the map.
|
||||||
|
|
||||||
If a subclass of dict defines a method :meth:`__missing__`, if the key *key*
|
.. 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*
|
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
|
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
|
returned or raised by the ``__missing__(key)`` call.
|
||||||
present. No other operations or methods invoke :meth:`__missing__`. If
|
No other operations or methods invoke :meth:`__missing__`. If
|
||||||
:meth:`__missing__` is not defined, :exc:`KeyError` is raised.
|
:meth:`__missing__` is not defined, :exc:`KeyError` is raised.
|
||||||
:meth:`__missing__` must be a method; it cannot be an instance variable::
|
:meth:`__missing__` must be a method; it cannot be an instance variable::
|
||||||
|
|
||||||
|
|
@ -3779,8 +3781,9 @@ pairs within braces, for example: ``{'jack': 4098, 'sjoerd': 4127}`` or ``{4098:
|
||||||
>>> c['red']
|
>>> c['red']
|
||||||
1
|
1
|
||||||
|
|
||||||
See :class:`collections.Counter` for a complete implementation including
|
The example above shows part of the implementation of
|
||||||
other methods helpful for accumulating and managing tallies.
|
:class:`collections.Counter`. A different ``__missing__`` method is used
|
||||||
|
by :class:`collections.defaultdict`.
|
||||||
|
|
||||||
.. describe:: d[key] = value
|
.. describe:: d[key] = value
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1904,6 +1904,12 @@ through the container; for mappings, :meth:`__iter__` should be the same as
|
||||||
indexes to allow proper detection of the end of the sequence.
|
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)
|
.. method:: object.__setitem__(self, key, value)
|
||||||
|
|
||||||
Called to implement assignment to ``self[key]``. Same note as for
|
Called to implement assignment to ``self[key]``. Same note as for
|
||||||
|
|
|
||||||
|
|
@ -9474,6 +9474,10 @@ C-API
|
||||||
Documentation
|
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.
|
||||||
|
|
||||||
- Issue #13989: Document that GzipFile does not support text mode, and give a
|
- Issue #13989: Document that GzipFile does not support text mode, and give a
|
||||||
more helpful error message when opened with an invalid mode string.
|
more helpful error message when opened with an invalid mode string.
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue