Does converting from the mutable bytearray
type to the non-mutable bytes
type incur a copy? Is there any cost associated with it, or does the interpreter just treat it as an immutable byte sequence, like casting a char*
to a const char* const
in C++?
ba = bytearray()
ba.extend("some big long string".encode('utf-8'))
# Is this conversion free or expensive?
write_bytes(bytes(ba))
Does this differ between Python 3 where bytes
is its own type and Python 2.7 where bytes
is just an alias for str
?
A new copy is created, the buffer is not shared between the
bytesarray
and the newbytes
object, in either Python 2 or 3.You couldn't share it, as the
bytesarray
object could still be referenced elsewhere and mutate the value.For the details, see the
bytesobject.c
source code, where the buffer protocol is used to create a straight up copy of the data (viaPyBuffer_ToContiguous()
).Martjin is right. I just wanted to back that answer up with the cpython source.
Looking at the source for bytes here, first
bytes_new
is called, which will callPyBytes_FromObject
, which will call_PyBytes_FromBuffer
, which creates a new bytes object and callsPyBuffer_ToContiguous
(defined here). This callsbuffer_to_contiguous
, which is a memory copy function. The comment for the function reads:Thus, a call to bytes with a bytearray argument will copy the data.