Margin is right. I just wanted to support this answer with cpython source.
Looking at the byte source here , the first bytes_new
, which is called PyBytes_FromObject
, which is called _PyBytes_FromBuffer
, which creates a new byte object and calls PyBuffer_ToContiguous
(defined here ). This calls buffer_to_contiguous
, which is a memory copy function. Comment for function:
Copy src to the adjacent view. the order is one of "C", "F" (Fortran) or "A" (any). Assumptions: src has PyBUF_FULL information, src-> ndim> = 1, len (mem) == src-> len.
So calling bytes with bytearray will copy the data.
source share