blob: 034ac320bc88d0307448ce72a59848f32c59f8fb [file] [log] [blame]
Guido van Rossum5fdeeea1994-01-02 01:22:07 +00001\section{Built-in module \sectcode{array}}
2\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}
15\lineiii{'h'}{signed integer}{2}
16\lineiii{'i'}{signed integer}{2}
17\lineiii{'l'}{signed integer}{4}
18\lineiii{'f'}{floating point}{4}
19\lineiii{'d'}{floating point}{8}
20\end{tableiii}
21
22The actual representation of values is determined by the machine
Guido van Rossum16d6e711994-08-08 12:30:22 +000023architecture (strictly speaking, by the C implementation). The actual
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000024size can be accessed through the \var{typecode} attribute.
25
26The module defines the following function:
27
28\renewcommand{\indexsubitem}{(in module array)}
29
Guido van Rossum16d6e711994-08-08 12:30:22 +000030\begin{funcdesc}{array}{typecode\optional{\, initializer}}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000031Return a new array whose items are restricted by \var{typecode}, and
32initialized from the optional \var{initializer} value, which must be a
33list or a string. The list or string is passed to the new array's
34\code{fromlist()} or \code{fromstring()} method (see below) to add
35initial items to the array.
36\end{funcdesc}
37
38Array objects support the following data items and methods:
39
40\begin{datadesc}{typecode}
41The typecode character used to create the array.
42\end{datadesc}
43
44\begin{datadesc}{itemsize}
45The length in bytes of one array item in the internal representation.
46\end{datadesc}
47
48\begin{funcdesc}{append}{x}
49Append a new item with value \var{x} to the end of the array.
50\end{funcdesc}
51
52\begin{funcdesc}{byteswap}{x}
53``Byteswap'' all items of the array. This is only supported for
Guido van Rossum16d6e711994-08-08 12:30:22 +000054integer values. It is useful when reading data from a file written
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000055on a machine with a different byte order.
56\end{funcdesc}
57
58\begin{funcdesc}{fromfile}{f\, n}
59Read \var{n} items (as machine values) from the file object \var{f}
60and append them to the end of the array. If less than \var{n} items
61are available, \code{EOFError} is raised, but the items that were
62available are still inserted into the array.
63\end{funcdesc}
64
65\begin{funcdesc}{fromlist}{list}
66Appends items from the list. This is equivalent to
67\code{for x in \var{list}: a.append(x)}
68except that if there is a type error, the array is unchanged.
69\end{funcdesc}
70
71\begin{funcdesc}{fromstring}{s}
72Appends items from the string, interpreting the string as an
73array of machine values (i.e. as if it had been read from a
74file using the \code{fromfile()} method).
75\end{funcdesc}
76
77\begin{funcdesc}{insert}{i\, x}
78Insert a new item with value \var{x} in the array before position
79\var{i}.
80\end{funcdesc}
81
82\begin{funcdesc}{tofile}{f}
83Write all items (as machine values) to the file object \var{f}.
84\end{funcdesc}
85
86\begin{funcdesc}{tolist}{}
87Convert the array to an ordinary list with the same items.
88\end{funcdesc}
89
90\begin{funcdesc}{tostring}{}
91Convert the array to an array of machine values and return the
92string representation (the same sequence of bytes that would
93be written to a file by the \code{tofile()} method.)
94\end{funcdesc}
95
96When an array object is printed or converted to a string, it is
97represented as \code{array(\var{typecode}, \var{initializer})}. The
98\var{initializer} is omitted if the array is empty, otherwise it is a
99string if the \var{typecode} is \code{'c'}, otherwise it is a list of
100numbers. The string is guaranteed to be able to be converted back to
101an array with the same type and value using reverse quotes
102(\code{``}). Examples:
103
104\bcode\begin{verbatim}
105array('l')
106array('c', 'hello world')
107array('l', [1, 2, 3, 4, 5])
108array('d', [1.0, 2.0, 3.14])
109\end{verbatim}\ecode