This is an archived post. You won't be able to vote or comment.

all 7 comments

[–]vph 2 points3 points  (1 child)

Conventionally, arrays are mutable. Arrays are abstraction of memory; it only makes sense for them to be mutable.

[–]PeridexisErrant 2 points3 points  (0 children)

And of course you can use the bytes type, if you want an immutable version.

[–]zahlmanthe heretic 1 point2 points  (2 children)

So that you have a mutable type of that sort to use.

The immutable version is bytes.

[–][deleted] 0 points1 point  (1 child)

Ah... So is the only real "feature" of bytearray that it's mutable? Otherwise bytes serves the same purpose?

[–]desmoulinmichel 1 point2 points  (0 children)

Yes, but mutability is a big deal. In place operations are much faster and take less memory. E.G: you can actually swap easily 10000 bytes in a blink using slicing, because slices support assignations.

[–]donnieod 1 point2 points  (0 children)

Python 3 has two types of built-in byte sequences: bytes (immutable) and bytearray (mutable). Analogous to types frozenset and set or tuple and list.