diff options
author | Guido van Rossum <guido@python.org> | 2006-02-25 22:38:04 (GMT) |
---|---|---|
committer | Guido van Rossum <guido@python.org> | 2006-02-25 22:38:04 (GMT) |
commit | 1968ad32cd7f46d9bb64826672ef68cdaee35288 (patch) | |
tree | c46db5a446d9de18fb8436408ec29d2111a2f5ad /Doc/lib/libcollections.tex | |
parent | ab51f5f24d6f6edef5e8fac5e31b2e4ac0cbdbac (diff) | |
download | cpython-1968ad32cd7f46d9bb64826672ef68cdaee35288.zip cpython-1968ad32cd7f46d9bb64826672ef68cdaee35288.tar.gz cpython-1968ad32cd7f46d9bb64826672ef68cdaee35288.tar.bz2 |
- 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).
Diffstat (limited to 'Doc/lib/libcollections.tex')
-rw-r--r-- | Doc/lib/libcollections.tex | 50 |
1 files changed, 47 insertions, 3 deletions
diff --git a/Doc/lib/libcollections.tex b/Doc/lib/libcollections.tex index 51603aa..542ef6b 100644 --- a/Doc/lib/libcollections.tex +++ b/Doc/lib/libcollections.tex @@ -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} |