I want to write a bytearray
type into a memoryview
type. What I tried:
my_memory_view = memoryview(b'hello')
new_byte_string = bytearray(b'world')
my_memory_view = new_byte_string
but it returned:
AttributeError: can't set attribute
I know that it is possible to write into memoryview via:
my_memory_view[0] = 12 #Changes first byte
Is there a way to insert the values of the bytearray
automatically into memoryview
?
I made a mistake: the error is not AttributeError
the problem occurs because the type changes, but in my package I use(shared_memory
)
AttributeError
will be shown.
You can't write into your memoryview, because your memoryview is a view of a bytes object, which is immutable.
If you had a writable memoryview, such as memoryview(bytearray(b'hello'))
, you could do
your_memoryview[:] = whatever
to write the contents of whatever
into the underlying memory.