bytearray

A changeable sequence of bytes, often used as a buffer for binary input and output.

In more detail

Unlike bytes, a bytearray can be changed by index, with values from 0 to 255. A binary handle's readinto(buf) fills an existing bytearray and returns how many bytes it filled, so one buffer can be reused for every chunk. When fewer bytes remain than the buffer holds, only its front is overwritten.

Example

buf = bytearray(4)
with open('data.bin', 'rb') as f:
    n = f.readinto(buf)

Related terms

Taught in

References

Certification courses