summaryrefslogtreecommitdiffstats
path: root/Doc/lib/libcollections.tex
diff options
context:
space:
mode:
Diffstat (limited to 'Doc/lib/libcollections.tex')
-rw-r--r--Doc/lib/libcollections.tex66
1 files changed, 58 insertions, 8 deletions
diff --git a/Doc/lib/libcollections.tex b/Doc/lib/libcollections.tex
index 5a07a2d..33ace7d 100644
--- a/Doc/lib/libcollections.tex
+++ b/Doc/lib/libcollections.tex
@@ -9,14 +9,16 @@
This module implements high-performance container datatypes. Currently,
-there are two datatypes, deque and defaultdict.
+there are two datatypes, deque and defaultdict, and one datatype factory
+function, \function{NamedTuple}.
Future additions may include balanced trees and ordered dictionaries.
\versionchanged[Added defaultdict]{2.5}
+\versionchanged[Added NamedTuple]{2.6}
\subsection{\class{deque} objects \label{deque-objects}}
-\begin{funcdesc}{deque}{\optional{iterable}}
- Returns a new deque objected initialized left-to-right (using
+\begin{classdesc}{deque}{\optional{iterable}}
+ Returns a new deque object initialized left-to-right (using
\method{append()}) with data from \var{iterable}. If \var{iterable}
is not specified, the new deque is empty.
@@ -30,7 +32,7 @@ Future additions may include balanced trees and ordered dictionaries.
for \samp{pop(0)} and \samp{insert(0, v)} operations which change both the
size and position of the underlying data representation.
\versionadded{2.4}
-\end{funcdesc}
+\end{classdesc}
Deque objects support the following methods:
@@ -219,7 +221,7 @@ def maketree(iterable):
\subsection{\class{defaultdict} objects \label{defaultdict-objects}}
-\begin{funcdesc}{defaultdict}{\optional{default_factory\optional{, ...}}}
+\begin{classdesc}{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
@@ -231,7 +233,7 @@ def maketree(iterable):
passed to the \class{dict} constructor, including keyword arguments.
\versionadded{2.5}
-\end{funcdesc}
+\end{classdesc}
\class{defaultdict} objects support the following method in addition to
the standard \class{dict} operations:
@@ -254,11 +256,11 @@ the standard \class{dict} operations:
\class{defaultdict} objects support the following instance variable:
-\begin{datadesc}{default_factory}
+\begin{memberdesc}{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}
+\end{memberdesc}
\subsubsection{\class{defaultdict} Examples \label{defaultdict-examples}}
@@ -339,3 +341,51 @@ Setting the \member{default_factory} to \class{set} makes the
>>> d.items()
[('blue', set([2, 4])), ('red', set([1, 3]))]
\end{verbatim}
+
+
+
+\subsection{\function{NamedTuple} datatype factory function \label{named-tuple-factory}}
+
+\begin{funcdesc}{NamedTuple}{typename, fieldnames}
+ Returns a new tuple subclass named \var{typename}. The new subclass is used
+ to create tuple-like objects that have fields accessable by attribute
+ lookup as well as being indexable and iterable. Instances of the subclass
+ also have a helpful docstring (with typename and fieldnames) and a helpful
+ \method{__repr__()} method which lists the tuple contents in a \code{name=value}
+ format.
+ \versionadded{2.6}
+
+ The \var{fieldnames} are specified in a single string and are separated by spaces.
+ Any valid Python identifier may be used for a field name.
+
+ Example:
+ \begin{verbatim}
+>>> Point = NamedTuple('Point', 'x y')
+>>> Point.__doc__ # docstring for the new datatype
+'Point(x, y)'
+>>> p = Point(11, y=22) # instantiate with positional or keyword arguments
+>>> p[0] + p[1] # works just like the tuple (11, 22)
+33
+>>> x, y = p # unpacks just like a tuple
+>>> x, y
+(11, 22)
+>>> p.x + p.y # fields also accessable by name
+33
+>>> p # readable __repr__ with name=value style
+Point(x=11, y=22)
+\end{verbatim}
+
+ The use cases are the same as those for tuples. The named factories
+ assign meaning to each tuple position and allow for more readable,
+ self-documenting code. Named tuples can also be used to assign field names
+ to tuples
+ returned by the \module{csv} or \module{sqlite3} modules. For example:
+
+ \begin{verbatim}
+import csv
+EmployeeRecord = NamedTuple('EmployeeRecord', 'name age title department paygrade')
+for tup in csv.reader(open("employees.csv", "rb")):
+ print EmployeeRecord(*tup)
+\end{verbatim}
+
+\end{funcdesc}