Extract Bytes from bytearray with take_bytes in Python 3.15

Extract Bytes from bytearray with take_bytes in Python 3.15

A bytearray can be modified after it is created, so it is a convenient place to collect data piece by piece. When the data is complete, it usually has to be turned into a bytes object, which cannot be modified. That conversion copies every byte, so both objects sit in memory at the same time. For a small buffer this costs little, but with large amounts of data the copy takes noticeable time and doubles the memory needed.

For example, producing a bytes object and releasing the buffer afterwards takes two statements:

buffer = bytearray(b'Hello world')

data = bytes(buffer)
buffer.clear()

print(data)  # b'Hello world'
print(buffer)  # bytearray(b'')

The call to bytes duplicates every byte, and clear is needed afterwards to release the original. This pattern is common in code that reads sockets, decodes streams, or builds payloads.

Since Python 3.15, the take_bytes method removes the data from the bytearray and returns it as a bytes object in a single step.

buffer = bytearray(b'Hello world')

data = buffer.take_bytes()

print(data)  # b'Hello world'
print(buffer)  # bytearray(b'')

The result is the same, but no copy is made. The new bytes object reuses the memory previously used by the bytearray. For large buffers this removes both costs - the time spent copying and the extra memory. The bytearray is left empty, so the explicit clear is no longer needed.

The method can also take a byte count, removing that many bytes from the front and leaving the rest untouched.

buffer = bytearray(b'line1\nline2\n')

first = buffer.take_bytes(6)

print(first)  # b'line1\n'
print(buffer)  # bytearray(b'line2\n')

This replaces the usual two-step process of copying a slice and then deleting it to remove one record while keeping the rest of the buffer.

Leave a Comment

Cancel reply

Your email address will not be published.