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 | |
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')
-rw-r--r-- | Doc/lib/libcollections.tex | 50 | ||||
-rw-r--r-- | Doc/lib/libcopy.tex | 7 | ||||
-rw-r--r-- | Doc/lib/libstdtypes.tex | 13 |
3 files changed, 64 insertions, 6 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} diff --git a/Doc/lib/libcopy.tex b/Doc/lib/libcopy.tex index d73d6fd..5964187 100644 --- a/Doc/lib/libcopy.tex +++ b/Doc/lib/libcopy.tex @@ -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 diff --git a/Doc/lib/libstdtypes.tex b/Doc/lib/libstdtypes.tex index 5df39db..5d15375 100644 --- a/Doc/lib/libstdtypes.tex +++ b/Doc/lib/libstdtypes.tex @@ -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 |