I would like to allocate a large bytearray, but also divide it up into a number of smaller bytearray-like objects. When the smaller objects are modified, they should modify the underlying bytearray.
I’ve tried both of the following strategies.
from pprint import pprint as pp
b = bytearray(10)
c = b[5:]
c = bytearray(1 for d in c)
pp(c)
pp(b)
and
from pprint import pprint as pp
b = bytearray(10)
c = b[5:]
for i in range(len(c)):
c[i] = 1
pp(c)
pp(b)
They both output
bytearray(b'\x01\x01\x01\x01\x01')
bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')
My aim would be to have a similar piece of code, which outputs
bytearray(b'\x01\x01\x01\x01\x01')
bytearray(b'\x00\x00\x00\x00\x00\x01\x01\x01\x01\x01')
That is, when c is updated, b is also updated in the appropriate position.
memoryview is close to what you want
you have to use
c.tobytes()or passcto bytearray to see it’s contents