home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The Datafile PD-CD 5
/
DATAFILE_PDCD5.iso
/
utilities
/
p
/
python
/
pyhtmldoc
/
a
/
array
< prev
Wrap
Text File
|
1996-11-14
|
5KB
|
100 lines
<TITLE>array -- Python library reference</TITLE>
Prev: <A HREF="../w/whrandom" TYPE="Prev">whrandom</A>
Up: <A HREF="../m/miscellaneous_services" TYPE="Up">Miscellaneous Services</A>
Top: <A HREF="../t/top" TYPE="Top">Top</A>
<H1>5.4. Built-in Module <CODE>array</CODE></H1>
This module defines a new object type which can efficiently represent
an array of basic values: characters, integers, floating point
numbers. Arrays are sequence types and behave very much like lists,
except that the type of objects stored in them is constrained. The
type is specified at object creation time by using a <DFN>type code</DFN>,
which is a single character. The following type codes are defined:
<P>
<DL>
<DT><I>Typecode</I><DD><I>Type</I> --- <I>Minimal size in bytes</I>
<P>
<DT><CODE>'c'</CODE><DD>character --- 1
<DT><CODE>'b'</CODE><DD>signed integer --- 1
<DT><CODE>'h'</CODE><DD>signed integer --- 2
<DT><CODE>'i'</CODE><DD>signed integer --- 2
<DT><CODE>'l'</CODE><DD>signed integer --- 4
<DT><CODE>'f'</CODE><DD>floating point --- 4
<DT><CODE>'d'</CODE><DD>floating point --- 8
</DL>
The actual representation of values is determined by the machine
architecture (strictly speaking, by the C implementation). The actual
size can be accessed through the <VAR>itemsize</VAR> attribute.
<P>
See also built-in module <CODE>struct</CODE>.
The module defines the following function:
<P>
<DL><DT><B>array</B> (<VAR>typecode</VAR>[, <VAR>initializer</VAR>]) -- function of module array<DD>
Return a new array whose items are restricted by <VAR>typecode</VAR>, and
initialized from the optional <VAR>initializer</VAR> value, which must be a
list or a string. The list or string is passed to the new array's
<CODE>fromlist()</CODE> or <CODE>fromstring()</CODE> method (see below) to add
initial items to the array.
</DL>
Array objects support the following data items and methods:
<P>
<DL><DT><B>typecode</B> -- data of module array<DD>
The typecode character used to create the array.
</DL>
<DL><DT><B>itemsize</B> -- data of module array<DD>
The length in bytes of one array item in the internal representation.
</DL>
<DL><DT><B>append</B> (<VAR>x</VAR>) -- function of module array<DD>
Append a new item with value <VAR>x</VAR> to the end of the array.
</DL>
<DL><DT><B>byteswap</B> (<VAR>x</VAR>) -- function of module array<DD>
``Byteswap'' all items of the array. This is only supported for
integer values. It is useful when reading data from a file written
on a machine with a different byte order.
</DL>
<DL><DT><B>fromfile</B> (<VAR>f</VAR>, <VAR>n</VAR>) -- function of module array<DD>
Read <VAR>n</VAR> items (as machine values) from the file object <VAR>f</VAR>
and append them to the end of the array. If less than <VAR>n</VAR> items
are available, <CODE>EOFError</CODE> is raised, but the items that were
available are still inserted into the array. <VAR>f</VAR> must be a real
built-in file object; something else with a <CODE>read()</CODE> method won't
do.
</DL>
<DL><DT><B>fromlist</B> (<VAR>list</VAR>) -- function of module array<DD>
Append items from the list. This is equivalent to
<CODE>for x in <VAR>list</VAR>: a.append(x)</CODE>
except that if there is a type error, the array is unchanged.
</DL>
<DL><DT><B>fromstring</B> (<VAR>s</VAR>) -- function of module array<DD>
Appends items from the string, interpreting the string as an
array of machine values (i.e. as if it had been read from a
file using the <CODE>fromfile()</CODE> method).
</DL>
<DL><DT><B>insert</B> (<VAR>i</VAR>, <VAR>x</VAR>) -- function of module array<DD>
Insert a new item with value <VAR>x</VAR> in the array before position
<VAR>i</VAR>.
</DL>
<DL><DT><B>tofile</B> (<VAR>f</VAR>) -- function of module array<DD>
Write all items (as machine values) to the file object <VAR>f</VAR>.
</DL>
<DL><DT><B>tolist</B> () -- function of module array<DD>
Convert the array to an ordinary list with the same items.
</DL>
<DL><DT><B>tostring</B> () -- function of module array<DD>
Convert the array to an array of machine values and return the
string representation (the same sequence of bytes that would
be written to a file by the <CODE>tofile()</CODE> method.)
</DL>
When an array object is printed or converted to a string, it is
represented as <CODE>array(<VAR>typecode</VAR>, <VAR>initializer</VAR>)</CODE>. The
<VAR>initializer</VAR> is omitted if the array is empty, otherwise it is a
string if the <VAR>typecode</VAR> is <CODE>'c'</CODE>, otherwise it is a list of
numbers. The string is guaranteed to be able to be converted back to
an array with the same type and value using reverse quotes
(<CODE>``</CODE>). Examples:
<P>
<UL COMPACT><CODE>array('l')<P>
array('c', 'hello world')<P>
array('l', [1, 2, 3, 4, 5])<P>
array('d', [1.0, 2.0, 3.14])<P>
</CODE></UL>