array — 0.2.0

org.pycforge.array · Author: PyCForge

License: GPL-3.0-only · Created: 2026-11-26

Runtime conversion · Direct declared calls

Using this import · Functions · Conversion contract · Worked examples

Function index

array

Overview

Use compact arrays for samples, counters and binary records.

Using this import

Run ordinary Python with an import of the reference library. For conversion, add * directly before import. The constructor, receiver methods and indexes keep their spelling.

Author's limitations

Runtime supports native b/B (signed/unsigned char), h/H (short) and i/I (int). Use integer and bool elements, negative indexes, owned slice copies, same-type slice replacement, deletion, concatenation and integer repetition. Methods accept positional arguments: append, extend, fromlist, insert, pop, index, count, remove, reverse, byteswap, frombytes, tobytes, tolist, fromfile and tofile. File methods use Runtime file streams. typecode and itemsize are read-only. Native width and byte order govern the stored bytes. Exported memoryviews, arbitrary integer protocols, other typecodes and Typed conversion are outside this profile.

Release notes

This version adds signed and unsigned byte, short and int storage, editing, byte order and binary file methods.

Conversion contract

Use *import array in the module preamble. Each signature states accepted positions, names and defaults. A slash marks positional-only parameters; a star marks keyword-only parameters. Argument expansion, function values and Typed conversion are unsupported.

Runtime supports native b/B (signed/unsigned char), h/H (short) and i/I (int). Use integer and bool elements, negative indexes, owned slice copies, same-type slice replacement, deletion, concatenation and integer repetition. Methods accept positional arguments: append, extend, fromlist, insert, pop, index, count, remove, reverse, byteswap, frombytes, tobytes, tolist, fromfile and tofile. File methods use Runtime file streams. typecode and itemsize are read-only. Native width and byte order govern the stored bytes. Exported memoryviews, arbitrary integer protocols, other typecodes and Typed conversion are outside this profile.

array.array(typecode[, initializer]) -> array

Construct a compact mutable array of native integers.

Normalized semantic slots: typecode, initializer. These slots are supplied by the engine; the signature above defines source arguments and defaults.

Assigning the result to another name shares the same array. Initialize from integer iterables or native-order bytes. Read with negative indexes or slices. Use append, extend, frombytes, tobytes and tolist. An iterable extension retains items accepted before a later item raises. fromlist commits the whole batch. Use insert, pop, bounded index, count, remove and reverse to edit or search. Slice assignment needs a matching typecode. byteswap reverses the bytes in each element.

Argument 1: typecode

Argument 2: initializer

Returns: An independent mutable array.

View example: edit-sample-batch

Worked examples

Compare your run with the expected output below. The SDK checks conversion when authoring a package. Opening Help and registering a package do not execute the examples.

Example: edit-sample-batch

*import array as buf
def main():
    batch = buf.array('B', [10])
    stream = buf.array('B', [10])
    try:
        batch.fromlist([20, 256])
    except OverflowError:
        pass
    try:
        stream.extend([20, 256])
    except OverflowError:
        pass
    print(batch.tolist(), stream.tolist())
    stream.insert(0, 5)
    print(stream.index(20, 1), stream.pop(), stream.tolist())
    return 0

Expected standard output:

[10] [10, 20]
2 20 [5, 10]

Expected exit code: 0