Casting and module "array"

Edward C. Jones edcjones at
Sun Dec 31 01:05:27 EST 2000

Is there a better way to cast arrays made from module "array"?
Turning a large array into a list would seem to be slow.

import array

def cast(typecode, arr):
    return array.array(typecode, list(arr))

if __name__ == '__main__':
    Barr = array.array('B', [0, 1, 254, 255])
    harr = cast('h', Barr)
    print list(harr)
    barr = cast('b', Barr)  # Fails.

I would use "astype" from Numeric (aka NumPy) instead but NumPy
doesn't support unsigned short or unsigned long.

Ed Jones

More information about the Python-list mailing list