bytes vs bytearray in Python 2.6 and 3

For (at least) Python 3.7

According to the docs:

bytes objects are immutable sequences of single bytes

bytearray objects are a mutable counterpart to bytes objects.

And that’s pretty much it as far as bytes vs bytearray. In fact, they’re fairly interchangeable and designed to flexible enough to be mixed in operations without throwing errors. In fact, there is a whole section in the official documentation dedicated to showing the similarities between the bytes and bytearray apis.

Some clues as to why from the docs:

Since many major binary protocols are based on the ASCII text encoding, bytes objects offer several methods that are only valid when working with ASCII compatible data and are closely related to string objects in a variety of other ways.

Leave a Comment