[Python-Dev] A better and more basic array type

Dennis Heuer dh at triple-media.com
Thu Apr 27 16:01:43 CEST 2006

Yes, this was previously "inheriting basic types more efficiently" but
now I want something different ;)

I looked at the array type and found it quite C-ish. It is also not
suited for arithmetics because it's a sequence type like a constrained
list and not efficiently (and comfortably) usable like a *sliceable*
integer. I'd rather like to find a basic built-in array type in python,
which doesn't even ask for the format but just deals with the technical
issues internally. This array type works like the long type but is
mutable and supports *slicing* (focusing on a bit-range in the
presentation of the number). The advantage of this array is that it is
fully supporting arithmetics--even on slices--, that there is no need
for boundary-checking, and that this mutable array type is actually
usable as a superclass for new types. The other type of array, the
constrained list, is inclusive:

x = array()
x += 255    # x now holds the number 255 or, in other words, the first
            # 8 bits are set to 1
x += "1010" # x interprets "1010" as a bit-pattern and adds 10
x.step(8)   # telling x that, from now on, a *unit* is 8-bit long
            # (for correct slicing and iteration; this is not
            # related to the real/internal array layout)
x[:] = 0    # clearing all bits or setting x to zero
f = open("binary file", "rb")
x += f.read()
for n in x: # n holds always the next 8 bit (a byte)
    <do something with n>
y = x[4:6]  # stores the fifth and the sixth byte in y
x.step(1)   # telling x that, from now on, a *unit* is 1-bit long
z = x[4:6]  # stores the fifth and the sixth bit in z
x[0:2] = z  # overwrites the first two bits in x with the two bits in z

Possibly the step attribute can become problematic and it's better to
define a fix step at initialization. Smaller bit-packages from otherwise
compatible types could be packed up with trailing zeroes.

I hope that somebody agrees and is already starting to implement this
new array type. My best wishes are with you.

More information about the Python-Dev mailing list