Index: library/collections.rst =================================================================== --- library/collections.rst (revision 83761) +++ library/collections.rst (working copy) @@ -557,49 +557,48 @@ .. class:: defaultdict([default_factory[, ...]]) - Returns a new dictionary-like object. :class:`defaultdict` is a subclass of the - built-in :class:`dict` class. It overrides one method and adds one writable - instance variable. The remaining functionality is the same as for the - :class:`dict` class and is not documented here. + :class:`defaultdict` is a :class:`dict` subclass that can guarantee + success on key lookups: if a key does not currently exist in a + ``defaultdict`` object, a "default value factory" is called to + provide a value for that key. The "default value factory" is + a callable object (typically, a function) that takes no + arguments. You specify this callable as the first argument to + ``defaultdict()``; additional arguments are the + same as for :class:`dict()`. + + The "default value factory" callable is stored as an + attribute, :attr:`default_factory`, of the newly created defaultdict + object. If you call ``defaultdict()`` with no arguments, or with + ``None`` as the first argument, the :attr:`default_factory` attribute is + set to ``None``. You can reassign the :attr:`default_factory` attribute + of an existing ``defaultdict`` object to another callable, or to + ``None``. + + When a lookup of a non-existent key is performed in a + ``defaultdict`` object, its :attr:`default_factory` attribute is + evaluated, and the resulting object is called (unless it + is ``None``): + + * If the call produces a value, that value is returned as the + result of the lookup. In addition, the key-value pair is + inserted into the ``defaultdict``. - The first argument provides the initial value for the :attr:`default_factory` - attribute; it defaults to ``None``. All remaining arguments are treated the same - as if they were passed to the :class:`dict` constructor, including keyword - arguments. + * If the call raises an exception, the exception is propagated + unchanged. + * If the :attr:`default_factory` attribute evaluates to + ``None``, a :exc:`KeyError` exception is raised, with the + non-existent key as its argument. (The ``defaultdict`` behaves + exactly like a standard :class:`dict` in this case.) + .. versionadded:: 2.5 - :class:`defaultdict` objects support the following method in addition to the - standard :class:`dict` operations: + If the ``defaultdict`` class does not provide exactly the + functionality you need, consider creating your own subclass of + :class:`dict` that implements the special method + :meth:`__missing__`. See the description of the :class:`dict` + class's "d[key]" operation for details. - - .. method:: defaultdict.__missing__(key) - - If the :attr:`default_factory` attribute is ``None``, this raises a - :exc:`KeyError` exception with the *key* as argument. - - If :attr:`default_factory` is not ``None``, it is called without arguments - to provide a default value for the given *key*, this value is inserted in - the dictionary for the *key*, and returned. - - If calling :attr:`default_factory` raises an exception this exception is - propagated unchanged. - - This method is called by the :meth:`__getitem__` method of the - :class:`dict` class when the requested key is not found; whatever it - returns or raises is then returned or raised by :meth:`__getitem__`. - - - :class:`defaultdict` objects support the following instance variable: - - - .. attribute:: defaultdict.default_factory - - This attribute is used by the :meth:`__missing__` method; it is - initialized from the first argument to the constructor, if present, or to - ``None``, if absent. - - :class:`defaultdict` Examples ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^