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

Amaury Forgeot d'Arc amauryfa at gmail.com
Wed Apr 15 03:50:06 CEST 2009


On Wed, Apr 15, 2009 at 03:01, Dan Eloff <dan.eloff at gmail.com> wrote:
> 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?

No, the read() method did not change from the 2.x series.
It returns a new object on each call.

> 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.

Files opened in binary mode have a readinto() method, which fills the
given bytearray.
Is this what you are looking for?

Amaury Forgeot d'Arc

More information about the Python-Dev mailing list