This module defines a new object type which can efficiently representan array of basic values: characters, integers, floating pointnumbers. Arrays are sequence types and behave very muchlike lists, except that the type of objects stored in them isconstrained. The type is specified at object creation time by using atype code, which is a single character. The following typecodes are defined:
| Type code | C Type | Python Type | Minimum size in bytes |
|---|---|---|---|
'c' | char | character | 1 |
'b' | signed char | int | 1 |
'B' | unsigned char | int | 1 |
'h' | signed short | int | 2 |
'H' | unsigned short | int | 2 |
'i' | signed int | int | 2 |
'I' | unsigned int | long | 2 |
'l' | signed long | int | 4 |
'L' | unsigned long | long | 4 |
'f' | float | float | 4 |
'd' | double | float | 8 |
The actual representation of values is determined by the machinearchitecture (strictly speaking, by the C implementation). The actualsize can be accessed through theitemsize attribute. The valuesstored for'L' and'I' items will be represented asPython long integers when retrieved, because Python's plain integertype cannot represent the full range of C's unsigned (long) integers.
The module defines the following function and type object:
Array objects support the ordinary sequence operations ofindexing, slicing, concatenation, and multiplication. When usingslice assignment, the assigned value must be an array object with thesame type code; in all other cases,TypeError is raised.Array objects also implement the buffer interface, and may be usedwherever buffer objects are supported.
Array objects support the following data items and methods:
(address,length) giving the currentmemory address and the length in elements of the buffer used to holdarray's contents. The size of the memory buffer in bytes can becomputed asarray.buffer_info()[1] *array.itemsize. This is occasionally useful when working withlow-level (and inherently unsafe) I/O interfaces that require memoryaddresses, such as certainioctl() operations. Thereturned numbers are valid as long as the array exists and nolength-changing operations are applied to it.Note:When using array objects from code written in C orC++ (the only way to effectively make use of this information), itmakes more sense to use the buffer interface supported by arrayobjects. This method is maintained for backward compatibility andshould be avoided in new code. The buffer interface is documented inthePython/C API Reference Manual.
-1, so that by defaultthe last item is removed and returned.Readn items (as machine values) from the file objectfand append them to the end of the array. If less thann itemsare available,EOFError is raised, but the items that wereavailable are still inserted into the array.f must be a realbuilt-in file object; something else with aread() method won'tdo.
Write all items (as machine values) to the file objectf.
When an array object is printed or converted to a string, it isrepresented asarray(typecode,initializer). Theinitializer is omitted if the array is empty, otherwise it is astring if thetypecode is'c', otherwise it is a list ofnumbers. The string is guaranteed to be able to be converted back toan array with the same type and value using reverse quotes(``), so long as thearray() function has beenimported usingfrom array import array. Examples:
array('l')array('c', 'hello world')array('l', [1, 2, 3, 4, 5])array('d', [1.0, 2.0, 3.14])See Also:
| Python Library Reference |