- Patch 1433928:

- The copy module now "copies" function objects (as atomic objects).
  - dict.__getitem__ now looks for a __missing__ hook before raising
    KeyError.
  - Added a new type, defaultdict, to the collections module.
    This uses the new __missing__ hook behavior added to dict (see above).
This commit is contained in:
Guido van Rossum 2006-02-25 22:38:04 +00:00
parent ab51f5f24d
commit 1968ad32cd
12 changed files with 611 additions and 10 deletions

View file

@ -8,9 +8,10 @@
\versionadded{2.4}
This module implements high-performance container datatypes. Currently, the
only datatype is a deque. Future additions may include B-trees
and Fibonacci heaps.
This module implements high-performance container datatypes. Currently,
there are two datatypes, deque and defaultdict.
Future additions may include B-trees and Fibonacci heaps.
\versionchanged[Added defaultdict]{2.5}
\begin{funcdesc}{deque}{\optional{iterable}}
Returns a new deque objected initialized left-to-right (using
@ -211,3 +212,46 @@ def maketree(iterable):
[[[['a', 'b'], ['c', 'd']], [['e', 'f'], ['g', 'h']]]]
\end{verbatim}
\begin{funcdesc}{defaultdict}{\optional{default_factory\optional{, ...}}}
Returns a new dictionary-like object. \class{defaultdict} is a subclass
of the builtin \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.
The first argument provides the initial value for the
\member{default_factory} attribute; it defaults to \code{None}.
All remaining arguments are treated the same as if they were
passed to the \class{dict} constructor, including keyword arguments.
\versionadded{2.5}
\end{funcdesc}
\class{defaultdict} objects support the following method in addition to
the standard \class{dict} operations:
\begin{methoddesc}{__missing__}{key}
If the \member{default_factory} attribute is \code{None}, this raises
an \exception{KeyError} exception with the \var{key} as argument.
If \member{default_factory} is not \code{None}, it is called without
arguments to provide a default value for the given \var{key}, this
value is inserted in the dictionary for the \var{key}, and returned.
If calling \member{default_factory} raises an exception this exception
is propagated unchanged.
This method is called by the \method{__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 \method{__getitem__}.
\end{methoddesc}
\class{defaultdict} objects support the following instance variable:
\begin{datadesc}{default_factory}
This attribute is used by the \method{__missing__} method; it is initialized
from the first argument to the constructor, if present, or to \code{None},
if absent.
\end{datadesc}

View file

@ -67,9 +67,12 @@ set of components copied.
\end{itemize}
This version does not copy types like module, class, function, method,
This module does not copy types like module, method,
stack trace, stack frame, file, socket, window, array, or any similar
types.
types. It does ``copy'' functions and classes (shallow and deeply),
by returning the original object unchanged; this is compatible with
the way these are treated by the \module{pickle} module.
\versionchanged[Added copying functions]{2.5}
Classes can use the same interfaces to control copying that they use
to control pickling. See the description of module

View file

@ -1350,7 +1350,7 @@ arbitrary objects):
\begin{tableiii}{c|l|c}{code}{Operation}{Result}{Notes}
\lineiii{len(\var{a})}{the number of items in \var{a}}{}
\lineiii{\var{a}[\var{k}]}{the item of \var{a} with key \var{k}}{(1)}
\lineiii{\var{a}[\var{k}]}{the item of \var{a} with key \var{k}}{(1), (10)}
\lineiii{\var{a}[\var{k}] = \var{v}}
{set \code{\var{a}[\var{k}]} to \var{v}}
{}
@ -1454,6 +1454,17 @@ then is updated with those key/value pairs:
\versionchanged[Allowed the argument to be an iterable of key/value
pairs and allowed keyword arguments]{2.4}
\item[(10)] If a subclass of dict defines a method \method{__missing__},
if the key \var{k} is not present, the \var{a}[\var{k}] operation calls
that method with the key \var{k} as argument. The \var{a}[\var{k}]
operation then returns or raises whatever is returned or raised by the
\function{__missing__}(\var{k}) call if the key is not present.
No other operations or methods invoke \method{__missing__}().
If \method{__missing__} is not defined, \exception{KeyError} is raised.
\method{__missing__} must be a method; it cannot be an instance variable.
For an example, see \module{collections}.\class{defaultdict}.
\versionadded{2.5}
\end{description}
\subsection{File Objects