blob: 1b028b339d31d7102769b6753e081642ab089d0f [file] [log] [blame]
Guido van Rossum470be141995-03-17 16:07:09 +00001\section{Built-in Module \sectcode{array}}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +00002\bimodindex{array}
3\index{arrays}
4
5This module defines a new object type which can efficiently represent
6an array of basic values: characters, integers, floating point
7numbers. Arrays are sequence types and behave very much like lists,
8except that the type of objects stored in them is constrained. The
9type is specified at object creation time by using a \dfn{type code},
10which is a single character. The following type codes are defined:
11
12\begin{tableiii}{|c|c|c|}{code}{Typecode}{Type}{Minimal size in bytes}
13\lineiii{'c'}{character}{1}
14\lineiii{'b'}{signed integer}{1}
Guido van Rossumb0b81811997-01-03 19:20:52 +000015\lineiii{'B'}{unsigned integer}{1}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000016\lineiii{'h'}{signed integer}{2}
Guido van Rossumb0b81811997-01-03 19:20:52 +000017\lineiii{'H'}{unsigned integer}{2}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000018\lineiii{'i'}{signed integer}{2}
Guido van Rossumb0b81811997-01-03 19:20:52 +000019\lineiii{'I'}{unsigned integer}{2}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000020\lineiii{'l'}{signed integer}{4}
Guido van Rossumb0b81811997-01-03 19:20:52 +000021\lineiii{'L'}{unsigned integer}{4}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000022\lineiii{'f'}{floating point}{4}
23\lineiii{'d'}{floating point}{8}
24\end{tableiii}
25
26The actual representation of values is determined by the machine
Guido van Rossum16d6e711994-08-08 12:30:22 +000027architecture (strictly speaking, by the C implementation). The actual
Guido van Rossumb0b81811997-01-03 19:20:52 +000028size can be accessed through the \var{itemsize} attribute. The values
29stored for \code{'L'} and \code{'I'} items will be represented as
30Python long integers when retrieved, because Python's plain integer
31type can't represent the full range of C's unsigned (long) integers.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000032
Guido van Rossumecde7811995-03-28 13:35:14 +000033See also built-in module \code{struct}.
34\bimodindex{struct}
35
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000036The module defines the following function:
37
38\renewcommand{\indexsubitem}{(in module array)}
39
Guido van Rossum16d6e711994-08-08 12:30:22 +000040\begin{funcdesc}{array}{typecode\optional{\, initializer}}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000041Return a new array whose items are restricted by \var{typecode}, and
42initialized from the optional \var{initializer} value, which must be a
43list or a string. The list or string is passed to the new array's
44\code{fromlist()} or \code{fromstring()} method (see below) to add
45initial items to the array.
46\end{funcdesc}
47
48Array objects support the following data items and methods:
49
50\begin{datadesc}{typecode}
51The typecode character used to create the array.
52\end{datadesc}
53
54\begin{datadesc}{itemsize}
55The length in bytes of one array item in the internal representation.
56\end{datadesc}
57
58\begin{funcdesc}{append}{x}
59Append a new item with value \var{x} to the end of the array.
60\end{funcdesc}
61
62\begin{funcdesc}{byteswap}{x}
63``Byteswap'' all items of the array. This is only supported for
Guido van Rossum16d6e711994-08-08 12:30:22 +000064integer values. It is useful when reading data from a file written
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000065on a machine with a different byte order.
66\end{funcdesc}
67
68\begin{funcdesc}{fromfile}{f\, n}
69Read \var{n} items (as machine values) from the file object \var{f}
70and append them to the end of the array. If less than \var{n} items
71are available, \code{EOFError} is raised, but the items that were
Guido van Rossum470be141995-03-17 16:07:09 +000072available are still inserted into the array. \var{f} must be a real
73built-in file object; something else with a \code{read()} method won't
74do.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000075\end{funcdesc}
76
77\begin{funcdesc}{fromlist}{list}
Guido van Rossum6c4f0031995-03-07 10:14:09 +000078Append items from the list. This is equivalent to
79\code{for x in \var{list}:\ a.append(x)}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000080except that if there is a type error, the array is unchanged.
81\end{funcdesc}
82
83\begin{funcdesc}{fromstring}{s}
84Appends items from the string, interpreting the string as an
85array of machine values (i.e. as if it had been read from a
86file using the \code{fromfile()} method).
87\end{funcdesc}
88
89\begin{funcdesc}{insert}{i\, x}
90Insert a new item with value \var{x} in the array before position
91\var{i}.
92\end{funcdesc}
93
94\begin{funcdesc}{tofile}{f}
95Write all items (as machine values) to the file object \var{f}.
96\end{funcdesc}
97
98\begin{funcdesc}{tolist}{}
99Convert the array to an ordinary list with the same items.
100\end{funcdesc}
101
102\begin{funcdesc}{tostring}{}
103Convert the array to an array of machine values and return the
104string representation (the same sequence of bytes that would
105be written to a file by the \code{tofile()} method.)
106\end{funcdesc}
107
108When an array object is printed or converted to a string, it is
109represented as \code{array(\var{typecode}, \var{initializer})}. The
110\var{initializer} is omitted if the array is empty, otherwise it is a
111string if the \var{typecode} is \code{'c'}, otherwise it is a list of
112numbers. The string is guaranteed to be able to be converted back to
113an array with the same type and value using reverse quotes
114(\code{``}). Examples:
115
116\bcode\begin{verbatim}
117array('l')
118array('c', 'hello world')
119array('l', [1, 2, 3, 4, 5])
120array('d', [1.0, 2.0, 3.14])
121\end{verbatim}\ecode