Add more items
This commit is contained in:
parent
984bdd759e
commit
c7095843ae
|
@ -857,10 +857,29 @@ language.
|
||||||
|
|
||||||
\begin{itemize}
|
\begin{itemize}
|
||||||
|
|
||||||
|
\item The \class{dict} type has a new hook for letting subclasses
|
||||||
|
provide a default value when a key isn't contained in the dictionary.
|
||||||
|
When a key isn't found, the dictionary's
|
||||||
|
\method{__missing__(\var{key})}
|
||||||
|
method will be called. This hook is used to implement
|
||||||
|
the new \class{defaultdict} class in the \module{collections}
|
||||||
|
module. The following example defines a dictionary
|
||||||
|
that returns zero for any missing key:
|
||||||
|
|
||||||
|
\begin{verbatim}
|
||||||
|
class zerodict (dict):
|
||||||
|
def __missing__ (self, key):
|
||||||
|
return 0
|
||||||
|
|
||||||
|
d = zerodict({1:1, 2:2})
|
||||||
|
print d[1], d[2] # Prints 1, 2
|
||||||
|
print d[3], d[4] # Prints 0, 0
|
||||||
|
\end{verbatim}
|
||||||
|
|
||||||
\item The \function{min()} and \function{max()} built-in functions
|
\item The \function{min()} and \function{max()} built-in functions
|
||||||
gained a \code{key} keyword argument analogous to the \code{key}
|
gained a \code{key} keyword argument analogous to the \code{key}
|
||||||
argument for \method{sort()}. This argument supplies a function
|
argument for \method{sort()}. This argument supplies a function that
|
||||||
that takes a single argument and is called for every value in the list;
|
takes a single argument and is called for every value in the list;
|
||||||
\function{min()}/\function{max()} will return the element with the
|
\function{min()}/\function{max()} will return the element with the
|
||||||
smallest/largest return value from this function.
|
smallest/largest return value from this function.
|
||||||
For example, to find the longest string in a list, you can do:
|
For example, to find the longest string in a list, you can do:
|
||||||
|
@ -903,8 +922,6 @@ class C():
|
||||||
\end{verbatim}
|
\end{verbatim}
|
||||||
(Implemented by Brett Cannon.)
|
(Implemented by Brett Cannon.)
|
||||||
|
|
||||||
% XXX __missing__ hook in dictionaries
|
|
||||||
|
|
||||||
\end{itemize}
|
\end{itemize}
|
||||||
|
|
||||||
|
|
||||||
|
@ -964,9 +981,6 @@ details.
|
||||||
|
|
||||||
\begin{itemize}
|
\begin{itemize}
|
||||||
|
|
||||||
% XXX collections.deque now has .remove()
|
|
||||||
% collections.defaultdict
|
|
||||||
|
|
||||||
% the cPickle module no longer accepts the deprecated None option in the
|
% the cPickle module no longer accepts the deprecated None option in the
|
||||||
% args tuple returned by __reduce__().
|
% args tuple returned by __reduce__().
|
||||||
|
|
||||||
|
@ -984,6 +998,55 @@ details.
|
||||||
and the code for u-LAW encoding has been improved. (Contributed by
|
and the code for u-LAW encoding has been improved. (Contributed by
|
||||||
Lars Immisch.)
|
Lars Immisch.)
|
||||||
|
|
||||||
|
\item The \module{collections} module gained a new type,
|
||||||
|
\class{defaultdict}, that subclasses the standard \class{dict}
|
||||||
|
type. The new type mostly behaves like a dictionary but constructs a
|
||||||
|
default value when a key isn't present, automatically adding it to the
|
||||||
|
dictionary for the requested key value.
|
||||||
|
|
||||||
|
The first argument to \class{defaultdict}'s constructor is a factory
|
||||||
|
function that gets called whenever a key is requested but not found.
|
||||||
|
This factory function receives no arguments, so you can use built-in
|
||||||
|
type constructors such as \function{list()} or \function{int()}. For
|
||||||
|
example,
|
||||||
|
you can make an index of words based on their initial letter like this:
|
||||||
|
|
||||||
|
\begin{verbatim}
|
||||||
|
words = """Nel mezzo del cammin di nostra vita
|
||||||
|
mi ritrovai per una selva oscura
|
||||||
|
che la diritta via era smarrita""".lower().split()
|
||||||
|
|
||||||
|
index = defaultdict(list)
|
||||||
|
|
||||||
|
for w in words:
|
||||||
|
init_letter = w[0]
|
||||||
|
index[init_letter].append(w)
|
||||||
|
\end{verbatim}
|
||||||
|
|
||||||
|
Printing \code{index} results in the following output:
|
||||||
|
|
||||||
|
\begin{verbatim}
|
||||||
|
defaultdict(<type 'list'>, {'c': ['cammin', 'che'], 'e': ['era'],
|
||||||
|
'd': ['del', 'di', 'diritta'], 'm': ['mezzo', 'mi'],
|
||||||
|
'l': ['la'], 'o': ['oscura'], 'n': ['nel', 'nostra'],
|
||||||
|
'p': ['per'], 's': ['selva', 'smarrita'],
|
||||||
|
'r': ['ritrovai'], 'u': ['una'], 'v': ['vita', 'via']}
|
||||||
|
\end{verbatim}
|
||||||
|
|
||||||
|
The \class{deque} double-ended queue type supplied by the
|
||||||
|
\module{collections} module now has a \method{remove(\var{value})}
|
||||||
|
method that removes the first occurrence of \var{value} in the queue,
|
||||||
|
raising \exception{ValueError} if the value isn't found.
|
||||||
|
|
||||||
|
\item The \module{cProfile} module is a C implementation of
|
||||||
|
the existing \module{profile} module that has much lower overhead.
|
||||||
|
The module's interface is the same as \module{profile}: you run
|
||||||
|
\code{cProfile.run('main()')} to profile a function, can save profile
|
||||||
|
data to a file, etc. It's not yet known if the Hotshot profiler,
|
||||||
|
which is also written in C but doesn't match the \module{profile}
|
||||||
|
module's interface, will continue to be maintained in future versions
|
||||||
|
of Python. (Contributed by Armin Rigo.)
|
||||||
|
|
||||||
\item In the \module{gc} module, the new \function{get_count()} function
|
\item In the \module{gc} module, the new \function{get_count()} function
|
||||||
returns a 3-tuple containing the current collection counts for the
|
returns a 3-tuple containing the current collection counts for the
|
||||||
three GC generations. This is accounting information for the garbage
|
three GC generations. This is accounting information for the garbage
|
||||||
|
@ -1141,9 +1204,6 @@ by some specifications, so it's still available as
|
||||||
%======================================================================
|
%======================================================================
|
||||||
% whole new modules get described in subsections here
|
% whole new modules get described in subsections here
|
||||||
|
|
||||||
%======================================================================
|
|
||||||
%\subsection{The cProfile module}
|
|
||||||
|
|
||||||
%======================================================================
|
%======================================================================
|
||||||
\subsection{The ctypes package}
|
\subsection{The ctypes package}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue