[Python-Dev] Why does read() return bytes instead of bytearray?

Dan Eloff dan.eloff at gmail.com
Wed Apr 15 03:01:55 CEST 2009


Hi,

Can someone please explain why read() should return an immutable bytes
type instead of a mutable bytearray? It's not like read() from a file
and use buffer as a key in a dict is common. Certainly read() from
file or stream, modify, write is very common. I don't understand why
the common case pays the price in performance and simplicity. It
seemed to me that the immutable bytes was described as being useful in
niche situations, but it actually seems to have been favored over
bytearray in Python 3.

Was there was a good reason for this decision? Or was this just an
artifact in the change to two bytes types?

The reason I ask is I have a server application that is mostly stream
reading/writing on the hot path and in Python 2.5 the redundant copies
add up to a significant overhead, (I estimate as much as 25% from my
measurements) I was looking at Python 3 as a way to solve that
problem, but unfortunately it doesn't look like it will help.

Thanks,
-Dan


More information about the Python-Dev mailing list